多语句事务中不允许使用 ALTER DATABASE 语句
ALTER DATABASE statement not allowed within multi-statement transaction
我写了一个数据迁移脚本,我在其中检查以前的数据迁移是否正在进行然后关闭所有数据库连接这意味着将数据库模式从 MULTI_USER
模式更改为 RESTRICTED_USER
模式
通过 donig 所以我的数据库将继续受限模式并且在更改数据库模式后只允许授权连接我正在更新我的一些列然后我将数据库恢复到多用户模式
但是当我 运行 它给了我以下错误
Msg 6401, Level 16, State 1, Procedure SP_CollegeMigration, Line 234
Cannot roll back CollegeMigration. No transaction or savepoint of that name was found.
Msg 226, Level 16, State 6, Procedure SP_CollegeMigration, Line 236
ALTER DATABASE statement not allowed within multi-statement transaction.
Msg 50000, Level 16, State 6, Procedure SP_CollegeMigration, Line 260
ALTER DATABASE statement not allowed within multi-statement transaction.
这是我的一些脚本
IF(@preMigrationStatus = 'InProcess' and @isForceFull = 'Yes')
BEGIN
/*if the previous migration is in process then inside if i am changing the
db mode to restricted_user mode
*/
SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
UPDATE TblCollegeMigrationHistory
SET MigrationStatus ='Failed',
Comments = Comments + 'Migration Failed at '+ Convert(nvarchar, getdate()) + ' ;'
WHERE HistoryID = @HistoryId;
ALTER DATABASE AlAhsaan2014 SET MULTI_USER WITH ROLLBACK IMMEDIATE;
SET IMPLICIT_TRANSACTIONS ON
/* select statement goes here */
END
我解决了这个问题
通过创建一个新的 storedprocedure
并将这两行放入过程
SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
然后从我的脚本中调用存储过程
IF(@preMigrationStatus = 'InProcess' and @isForceFull = 'Yes')
BEGIN
/*if the previous migration is in process then inside if i am changing the
db mode to restricted_user mode
*/
exec sp_changeDbMode
UPDATE TblCollegeMigrationHistory
SET MigrationStatus ='Failed',
Comments = Comments + 'Migration Failed at '+ Convert(nvarchar, getdate()) + ' ;'
WHERE HistoryID = @HistoryId;
ALTER DATABASE AlAhsaan2014 SET MULTI_USER WITH ROLLBACK IMMEDIATE;
SET IMPLICIT_TRANSACTIONS ON
/* select statement goes here */
END
我写了一个数据迁移脚本,我在其中检查以前的数据迁移是否正在进行然后关闭所有数据库连接这意味着将数据库模式从 MULTI_USER
模式更改为 RESTRICTED_USER
模式
通过 donig 所以我的数据库将继续受限模式并且在更改数据库模式后只允许授权连接我正在更新我的一些列然后我将数据库恢复到多用户模式
但是当我 运行 它给了我以下错误
Msg 6401, Level 16, State 1, Procedure SP_CollegeMigration, Line 234
Cannot roll back CollegeMigration. No transaction or savepoint of that name was found.Msg 226, Level 16, State 6, Procedure SP_CollegeMigration, Line 236
ALTER DATABASE statement not allowed within multi-statement transaction.Msg 50000, Level 16, State 6, Procedure SP_CollegeMigration, Line 260
ALTER DATABASE statement not allowed within multi-statement transaction.
这是我的一些脚本
IF(@preMigrationStatus = 'InProcess' and @isForceFull = 'Yes')
BEGIN
/*if the previous migration is in process then inside if i am changing the
db mode to restricted_user mode
*/
SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
UPDATE TblCollegeMigrationHistory
SET MigrationStatus ='Failed',
Comments = Comments + 'Migration Failed at '+ Convert(nvarchar, getdate()) + ' ;'
WHERE HistoryID = @HistoryId;
ALTER DATABASE AlAhsaan2014 SET MULTI_USER WITH ROLLBACK IMMEDIATE;
SET IMPLICIT_TRANSACTIONS ON
/* select statement goes here */
END
我解决了这个问题
通过创建一个新的 storedprocedure
并将这两行放入过程
SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
然后从我的脚本中调用存储过程
IF(@preMigrationStatus = 'InProcess' and @isForceFull = 'Yes')
BEGIN
/*if the previous migration is in process then inside if i am changing the
db mode to restricted_user mode
*/
exec sp_changeDbMode
UPDATE TblCollegeMigrationHistory
SET MigrationStatus ='Failed',
Comments = Comments + 'Migration Failed at '+ Convert(nvarchar, getdate()) + ' ;'
WHERE HistoryID = @HistoryId;
ALTER DATABASE AlAhsaan2014 SET MULTI_USER WITH ROLLBACK IMMEDIATE;
SET IMPLICIT_TRANSACTIONS ON
/* select statement goes here */
END