Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 多语句事务中不允许使用ALTER DATABASE语句_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql 多语句事务中不允许使用ALTER DATABASE语句

Sql 多语句事务中不允许使用ALTER DATABASE语句,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我编写了一个数据迁移脚本,在该脚本中,我检查上一次数据迁移是否正在进行,然后关闭所有数据库连接,这意味着将多用户模式的数据库模式更改为受限用户模式 通过donig,我的数据库将进入受限模式,在更改数据库模式后,只允许授权连接。我正在更新我的some列,然后将db恢复为多用户模式 但当我运行时,它会给我以下错误 Msg 6401,16级,状态1,程序SP_CollegeMigration,第234行 无法回滚集合迁移。找不到该名称的事务或保存点 Msg 226,16级,状态6,程序SP_集合,第2

我编写了一个数据迁移脚本,在该脚本中,我检查上一次数据迁移是否正在进行,然后关闭所有数据库连接,这意味着将多用户模式的数据库模式更改为受限用户模式 通过donig,我的数据库将进入受限模式,在更改数据库模式后,只允许授权连接。我正在更新我的some列,然后将db恢复为多用户模式

但当我运行时,它会给我以下错误

Msg 6401,16级,状态1,程序SP_CollegeMigration,第234行 无法回滚集合迁移。找不到该名称的事务或保存点

Msg 226,16级,状态6,程序SP_集合,第236行 多语句事务中不允许使用ALTER DATABASE语句

Msg 50000,16级,状态6,程序SP_集合,第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 
我解决了这个问题 通过创建一个新的storedprocedure并将这两行放入该过程中

SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE  AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;
然后从我的脚本中调用StoredProcess

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

-旧样式的逗号分隔表列表样式不应再使用,而是建议使用20多年前ANSI-92 SQL标准引入的正确ANSI JOIN语法。请用GOwhy我的问题被否决?我知道这是一种糟糕的样式,但我的问题不是关于样式或语法join我的问题是关于多语句中不允许使用ALTER DATABASE语句transaction@DavidG:使用上面声明的GO my变量不可访问并生成错误