March 30, 2009 at 1:55 pm
Hi masters,
I have a dought.
I know that if i have two querys like below:
Insert into table1 values (x,y,z)
Go
Update table1 set columnX=y where columnX=z
That the SQL Server will execute first, the Insert, and only then, the update.
This is because the two querys are separated by the “GO”.
I know too, that if the first query fails, and if the second one executes well, then the second query will be committed to the database.
What about if I have the two querys, without the GO in the meadle? Like below:
Insert into table1 values (x,y,z)
Update table1 set columnX=y where columnX=z
Note - I did not made an explicit “Begin transaction” on the beginning of the statement.
In this case, will the second query starts executing only, when the first one have already finished to execute? Or will the two querys start executing at the same time?
I know that in this case, if one of the querys execute with success, that it will be committed to the database, even if the other had no success.
But my question in this case is, will they start executing at the exact same time? Or the first query will execute first and only when the first finish, the second one starts executing?
What about, if the instructions (querys) ad an explicit BEGIN TRANSACTION at the beginning and a commit transaction on the end? Like below:
Begin transaction
Insert into table1 values (x,y,z)
Update table1 set columnX=y where columnX=z
If @@error =0
begin
Commit transaction
End
Else
Rollback transaction
The two querys inside the explicit transaction, will execute at the same time? Or first it’s executed the first query and only then, will be executed the second query?
I know that if one of the querys fail, in this case, none of the querys will be executed. Correct?
March 30, 2009 at 2:09 pm
pedro.ribeiro (3/30/2009)
In this case, will the second query starts executing only, when the first one have already finished to execute?
yes
Or first it’s executed the first query and only then, will be executed the second query?
Yes
SQL executes statements one by one in sequence, just like virtually any other language out there.
I know that if one of the querys fail, in this case, none of the querys will be executed. Correct?
No. There isn't sufficient error handling in there for that. The @@Error is only checking the error code of the second query. If the first throws an error, you'll never know about it
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 30, 2009 at 3:01 pm
Hi - tks for your reply and your help.
Ok i undesrtud that querys are only executed when the above one as been executed first.
But in this case, if the query the firts query is allways the fisrts to be executed and the second query can only be executed when the firts one has finished, what is the big diference between doin the query like:
March 30, 2009 at 3:02 pm
Hi - tks for your reply and your help.
Ok i undesrtud that querys are only executed when the above one as been executed first.
But in this case, if the firts query is allways the first to be executed and the second query can only be executed when the first one has finished, what is the big diference between doing the query like:
Insert into table1 values (x,y,z)
Go
Update table1 set columnX=y where columnX=z
or like this:
Insert into table1 values (x,y,z)
Update table1 set columnX=y where columnX=z
tks,
Pedro
March 30, 2009 at 3:17 pm
GO is a batch breaker. It indicates where the querying tool must break up batches to send to SQL Server. It is not a T-SQL command
DECLARE @i int
Set @i = 0
Select * from SomeTable
SELECT @ i -- will work
DECLARE @i int
Set @i = 0
Select * from SomeTable
GO
SELECT @ i -- will not work. Error must declare variable @i. It was declared in the previous batch, not the current one
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
March 30, 2009 at 3:23 pm
ok.
1) Some times i gain performance because of the GO in the meadle of the staments.
I don't know why....
Suppose the two querys mencionated befour.
Why does the GO (bacth breaker) brings more perfomance to que execution of the querys?
2) In the cenario below:
Insert into table1 values (x,y,z)
Update table1 set columnX=y where columnX=z
Note - No explicit "Begin transaction" on the beginning of the querys.
If the first query fails and the second executes well, will the second query be automatically committed to the database? even if the first one as failed (and therefour not been committed to the database)?
3) you told me that the @@error function only verifies the instruction befour it.
So , in that case, only one transaction would be rolledback, in case of error.
what about if i do the query like this:
begin transaction
Insert into table1 values (x,y,z)
Update table1 set columnX=y where columnX=z
commit transation
In this case, if one of the two queries fail? will the other be committed to the database? for instance , suppose query one executes well, but query two fails.
Would query one be committed to the database?
Tks for your help,
Pedro
March 31, 2009 at 2:09 am
pedro.ribeiro (3/30/2009)
ok.Why does the GO (bacth breaker) brings more perfomance to que execution of the querys?
It shouldn't. Have you tested and times it?
If the first query fails and the second executes well, will the second query be automatically committed to the database? even if the first one as failed (and therefour not been committed to the database)?
Yes. There's no reason it won't. There's no conditional logic to force a rollback
In this case, if one of the two queries fail? will the other be committed to the database? for instance , suppose query one executes well, but query two fails.
Would query one be committed to the database?
Yes. Again you've got no conditional logic for force a rollback if either of the queries fail. Your code instructs SQL to begin a transaction, run two queries and then commit. If you want conditional rollback, you have to state it.
http://sqlinthewild.co.za/index.php/2008/05/20/common-t-sql-mistakes/
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply