Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
sql-lover
Yak Posting Veteran
99 Posts |
Posted - 2012-09-10 : 11:44:36
|
Hi,An easy one. I just want to confirm.I know GO is a Microsoft stuff. But can I use it inside a transaction, assuming I'm running my T-SQL snippet from Management Studio? I understand it should be avoided if it's being called from a programming language code, as GO is not pure SQL. But I am going to run the script via Management Studio.Here's a pseudo code with what I'm sayingSET XACT_ABORT ONDo something hereGODo extra stuffGOSET XACT_ABORT OFF Or, will it create problems in case of a failure and a rollback is needed? Thanks, |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-09-10 : 12:06:16
|
transactions can cross batches no problem.And even across databases:BEGIN TRANUSE tempdbGOSELECT 'foo' AS [HAHA] INTO barGoUSE modelGOSELECT 'woo' AS [HOHO] INTO shoGOSELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE [TABLE_NAME] = 'bar'SELECT * FROM model.INFORMATION_SCHEMA.TABLES WHERE [TABLE_NAME] = 'sho'GOROLLBACKSELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE [TABLE_NAME] = 'bar'SELECT * FROM model.INFORMATION_SCHEMA.TABLES WHERE [TABLE_NAME] = 'sho' However, just because it *can* doesn't mean that you should.....Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-10 : 12:18:14
|
quote: Originally posted by sql-lover Hi,An easy one. I just want to confirm.I know GO is a Microsoft stuff. But can I use it inside a transaction, assuming I'm running my T-SQL snippet from Management Studio? I understand it should be avoided if it's being called from a programming language code, as GO is not pure SQL. But I am going to run the script via Management Studio.Here's a pseudo code with what I'm sayingSET XACT_ABORT ONDo something hereGODo extra stuffGOSET XACT_ABORT OFF Or, will it create problems in case of a failure and a rollback is needed? Thanks,
GO is just a batch separatorso it just starts a new batch. no problem in wrapping transaction over multiple batchessee this toohttp://visakhm.blogspot.com/2010/02/custom-batch-separator-in-t-sql.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|