Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


query execution order


query execution order

Author
Message
pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 982
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?
GilaMonster
GilaMonster
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72769 Visits: 44963
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 982
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:
pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 982
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72769 Visits: 44963
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


pedro.ribeiro
pedro.ribeiro
SSC-Enthusiastic
SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)SSC-Enthusiastic (163 reputation)

Group: General Forum Members
Points: 163 Visits: 982
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
GilaMonster
GilaMonster
SSC Guru
SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)SSC Guru (72K reputation)

Group: General Forum Members
Points: 72769 Visits: 44963
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

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search