我编写了一个数据迁移脚本,我在其中检查先前的数据迁移是否正在进行,然后关闭所有数据库连接,这意味着将数据库模式更改为MULTI_USER
模式为RESTRICTED_USER
模式
通过donig所以我的数据库将进入限制模式,只有在更改数据库模式后才允许autherised连接我正在更新我的某些列,之后我将db恢复到多用户模式
但是当我运行它时会给我以下错误
Msg 6401,Level 16,State 1,Procedure SP_CollegeMigration,Line 234
无法回滚CollegeMigration。未找到该名称的任何事务或保存点。Msg 226,Level 16,State 6,Procedure SP_CollegeMigration,236行
多语句事务中不允许使用ALTER DATABASE语句。Msg 50000,Level 16,State 6,Procedure SP_CollegeMigration,260行
多语句事务中不允许使用ALTER DATABASE语句。
这是我的一些剧本
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
答案 0 :(得分:0)
我解决了这个问题
通过创建一个新的storedprocedure
并将这两行放入过程
SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
然后从我的脚本中调用storedprocedure
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