0
votes

I wrote a data migration script in which I am checking if the previous data migration is in process then close all db connections this mean change the database mode which is MULTI_USER mode to RESTRICTED_USER mode by donig so my database will go on restricted mode and only autherised connection will be allowed after change the database mode I am updating my some column and after that I am restoring db to multi user mode

But when I run it gives me following error

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.

This is some of my script

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 
1
Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style should no longer be used and instead it is recommended to use the proper ANSI JOIN syntax introduced with the ANSI-92 SQL Standard (more than 20 years ago) - marc_s
Separate your statement batches with GO - DavidG
why my question down voted ? - Khurram Ali
@marc_s: i know this is bad style but my question was not about style or join my question was about ALTER DATABASE statement not allowed within multi-statement transaction - Khurram Ali
@DavidG: by using GO my variables which is declared above not accessable and generating errors - Khurram Ali

1 Answers

0
votes

I Solved this problem by creating a new storedprocedure and place these two lines into the procedure

SET IMPLICIT_TRANSACTIONS OFF
ALTER DATABASE  AlAhsaan2014 SET RESTRICTED_USER WITH ROLLBACK IMMEDIATE;

and then call the storedprocedure from my script

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