| Author |
Topic |
|
ScottyDoesKnow
Starting Member
18 Posts |
Posted - 2010-06-16 : 10:03:18
|
I really can't figure out how to use transactions correctly. My current problem is that when I run the following code it says "The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION."USE [PredData]GOBEGIN TRANSACTIONGO/****** Object: Table [dbo].[Accounts] Script Date: 03/04/2010 15:12:38 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOSET ANSI_PADDING ONGOCREATE TABLE [dbo].[Accounts]( [Id] **********, [Username] **********, [Password] **********, [UserLevel] **********, CONSTRAINT [PK_Accounts] PRIMARY KEY CLUSTERED ( [Id] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY], CONSTRAINT [UK_USERNAME] UNIQUE NONCLUSTERED ( [Username] ASC)WITH (PAD_INDEX = OFF, IGNORE_DUP_KEY = OFF) ON [PRIMARY]) ON [PRIMARY]GOSET ANSI_PADDING OFFGOCOMMIT TRANSACTION |
|
|
ScottyDoesKnow
Starting Member
18 Posts |
Posted - 2010-06-16 : 10:16:49
|
| And I know the transaction is useless in this case, but that's just because I have a bunch of these files and threw a transaction into all of them. It's not working in the others either. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-06-18 : 12:12:35
|
| GO is a batch separator. so issue is you've begin tran in one batch and commit in other. if you want all of them in same transaction whats the need of GO s in between------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-18 : 13:06:18
|
We doBEGIN TRANSACTIONGOCREATE TABLEGO... other DDL stuff ...GOCOMMIT or ROLLBACKin our rollout scripts and it works fine.HOWEVER ... if you execute a statement block (i.e. a section which one/more statements with a single GO at the end of it) then IF that sections fails THEN the very next thing you do MUST be a ROLLBACK.Otherwise you execute the next statement and there is an implicit rollback of EVERYTHING since the BEGIN TRANS, then the statement executes ... and any more that you run, but they are OUTSIDE a transaction block I replaced the "********" above with INT, changed COMMIT to ROLLBACK, and it ran fine for me (in Query Analyser) |
 |
|
|
ScottyDoesKnow
Starting Member
18 Posts |
Posted - 2010-06-21 : 12:32:01
|
| I forgot to mention that the GO statements are not actually used. When I use the sql file through C#, I split on the GO statements and send each statement on its own. Could this be causing the problem? Maybe I need to through in a GO between files or something? |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-21 : 13:45:59
|
I'm not sure what happens if your application sends "BEGIN TRANSACTION" as a single statement. Sure SQL will begin a transaction, but maybe it will NOT persist it to subsequent commands that you send.SendBEGIN TRANSACTIONas one statement, and thenSELECT @@TRANCOUNTas a second and see what you get for the Transaction Count. If it has not incremented then the BEGIN TRANSACTION statement is not holding a transaction open, and hence COMMIT or ROLLBACK as a transaction will complain that there is no "help open" transaction to be acted on.Example values:SET NOCOUNT ONSELECT [No T/A]=@@TRANCOUNTBEGIN TRANSACTIONSELECT [T/A Level 1]=@@TRANCOUNTBEGIN TRANSACTION Level2aSELECT [T/A Level 2a]=@@TRANCOUNTSAVE TRANSACTION Level2bSELECT [T/A Level 2b]=@@TRANCOUNTROLLBACK TRANSACTION Level2bSELECT [T/A Level2a again]=@@TRANCOUNTCOMMIT TRANSACTION Level2aSELECT [T/A Level1 again]=@@TRANCOUNTROLLBACKSELECT [Ground zero]=@@TRANCOUNTSET NOCOUNT OFF |
 |
|
|
ScottyDoesKnow
Starting Member
18 Posts |
Posted - 2010-06-21 : 16:58:13
|
| Alright I feel a little embarrassed. I never really bothered to look back at the rest of my implementation that was written a long while ago. I was splitting the files by GO statements and then opening a connection for each line, so obviously the transactions wouldn't work. Besides that, there was a simple way for me to just program the transactions in C#, since every file would use them in the same way.Thanks for all the help anyways though guys. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2010-06-21 : 17:03:01
|
quote: Originally posted by ScottyDoesKnow I forgot to mention that the GO statements are not actually used. When I use the sql file through C#, I split on the GO statements and send each statement on its own. Could this be causing the problem? Maybe I need to through in a GO between files or something?
Possibly. BEGIN TRANSACTION starts a local transaction for the connection issuing the statement (per BOL). So, it's possible that each statement you execute could be on a different connection (depending on how your program is set up).EDIT: Doh, too late.. |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-06-22 : 03:56:07
|
quote: Originally posted by ScottyDoesKnow Alright I feel a little embarrassed ...
Nah, you don't want to worry about that!! but you may need to worry that whenever you have seen "ERROR" in the past then there has been no effective ROLLBACK and whatever partial statements ran will all have been executed, and not rolled back ... Bummer! |
 |
|
|
|