Can We use GO
statement mutiple times in a SQL Transaction
. I am having a long T-SQL script and I want to run it in a SQL Transaction
. If all goes well then I will commit otherwise will rollback.
But, While running that query I got error like 'create function must be the only statement in the batch'
. As I am creating and dropping many Functions and Procedures in that.
I have not used GO
anywhere in the script. My question is that - Can I use multiple times GO
statement in that long script. Because, GO
creates a batch and if batch executes successfully first time but fails next time then will rollback transaction
statement be able to actually rollback that has been executed ?
Structure of my script looks like :
PRINT 'Transaction Started'
BEGIN TRY
BEGIN TRAN
Drop Function
....
....
Create Function
....
....
Drop Procedure
....
....
Lots of statements
....
....
COMMIT TRAN
PRINT 'Transaction Succeeded'
END TRY
BEGIN CATCH
PRINT 'Transaction Failed'
IF(@@TRANCOUNT > 0)
ROLLBACK TRAN
END CATCH
I am creating this script to migrate some changes from newDB to oldDB in a single script.