﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 7,2000 / T-SQL  / query execution order / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 21:24:35 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>[quote][b]pedro.ribeiro (3/30/2009)[/b][hr]ok.Why does the GO (bacth breaker) brings more perfomance to que execution of the querys?[/quote]It shouldn't. Have you tested and times it?[quote]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)?[/quote]Yes. There's no reason it won't. There's no conditional logic to force a rollback[quote]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?[/quote]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.[url]http://sqlinthewild.co.za/index.php/2008/05/20/common-t-sql-mistakes/[/url]</description><pubDate>Tue, 31 Mar 2009 02:09:35 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>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=zNote - 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=zcommit transationIn 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</description><pubDate>Mon, 30 Mar 2009 15:23:22 GMT</pubDate><dc:creator>pedro.ribeiro</dc:creator></item><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>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[code]DECLARE @i intSet @i = 0Select * from SomeTableSELECT @ i -- will workDECLARE @i intSet @i = 0Select * from SomeTableGOSELECT @ i -- will not work. Error must declare variable @i. It was declared in the previous batch, not the current one[/code]</description><pubDate>Mon, 30 Mar 2009 15:17:40 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>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)GoUpdate table1 set columnX=y where columnX=zor like this:Insert into table1 values (x,y,z)Update table1 set columnX=y where columnX=ztks,Pedro </description><pubDate>Mon, 30 Mar 2009 15:02:36 GMT</pubDate><dc:creator>pedro.ribeiro</dc:creator></item><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>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:</description><pubDate>Mon, 30 Mar 2009 15:01:01 GMT</pubDate><dc:creator>pedro.ribeiro</dc:creator></item><item><title>RE: query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>[quote][b]pedro.ribeiro (3/30/2009)[/b][hr]In this case, will the second query  starts executing only, when the first one have already finished to execute? [/quote]yes[quote] Or first it’s executed the first query and only then, will be executed the second query?[/quote]YesSQL executes statements one by one in sequence, just like virtually any other language out there.[quote]I know that if one of the querys fail, in this case, none of the querys will be executed. Correct?[/quote]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</description><pubDate>Mon, 30 Mar 2009 14:09:53 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>query execution order</title><link>http://www.sqlservercentral.com/Forums/Topic686473-8-1.aspx</link><description>Hi masters,      I have a dought.I know that if i have two querys like below:Insert into table1 values (x,y,z)GoUpdate table1  set columnX=y where columnX=zThat 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=zNote - 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=zIf @@error =0 begin     Commit transactionEndElse     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?</description><pubDate>Mon, 30 Mar 2009 13:55:11 GMT</pubDate><dc:creator>pedro.ribeiro</dc:creator></item></channel></rss>