Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase ««12

The Dodgy GO Statement Expand / Collapse
Author
Message
Posted Tuesday, February 21, 2006 9:21 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Monday, November 12, 2007 6:32 AM
Points: 62, Visits: 3

Wow, tough crowd today.  Must be a Monday thing since yesterday was a day off for many folks.

I use GO all the time in scripts, especially migration scripts as they have a mix of functions, stored procs, and SQL statements including drop and create statements many of which must be at the start of a batch.  Knowing where to put the GOs in the script make all the difference.

One thing about GO that wasn't apparant to me when I first started TSQL was that you couldn't use a local variable that was declared "across a GO".  For instance this script gives you an error:

--

DECLARE @myvar varchar(100)
GO

SET @myvar = 'Bob was here!'
SELECT @myvar

--

In such a short script it's easy to fix, but in larger scripts that need to process in a specific order, this limitation may require using temporary tables or other objects to hold values from one batch to the next.

 

Post #260268
Posted Tuesday, February 21, 2006 9:36 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, April 14, 2014 4:18 PM
Points: 1,276, Visits: 1,132
Yes, you mentioned that it's not a real Transact-SQL statement. I was expanding on what you said it "isn't" by explaining what it actually "is".

As JT noted above, a discussion of how the QA "GO" batch terminator affects scope would be a nice addition to this article.

Another nice addition would be the effect that "GO" has on scripts passed to SQL Server via other methods (such as .NET's SqlCommand).

And to answer the author's question (just in case it wasn't fully evident in the other posts):

Q: "...I don't understand why they allowed any executable statements after the end of stored procedure?"

A: "Because the statements after QA's 'GO' are a different batch."


Personally I think the author picked a good subject, but there are a lot of aspects he skipped over that would have made the article a lot more informative.
Post #260284
Posted Tuesday, February 21, 2006 3:20 PM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Thursday, March 13, 2014 10:40 AM
Points: 573, Visits: 107

A couple of points, just to confirm that GO isn't TSQL but a terminator used by DMO and SMO to parse batches of SQL. Whats more with SQLCMD you can put a number after the GO for the preceeding TSQL batch to be repeated.

The biggest issue I have seen is the deployment of sprocs. The easiest way is to concatenate all the files for the store procs together and run that combined file. However if one file doesn't have a go at the end and all the files start with a if exists drop statement you can end up with.

If exists (select 1 ....)

    drop proc myfirstProc

go

create proc myfirstProc

    begin

    --    do some stuff

    end

 

If exists (select 1 ....)

    drop proc mysecondProc

go

create proc mysecondProc

    begin

    -- Do some stuff

    end

go

If exists (select 1 ....)

    drop proc mythirdProc

go

create proc mythirdProc

    begin

    -- Do some stuff

    end 

What you find is that this is likely to run in, however the first time someone runs myFirstProc it will drop mysecondProc Not good.

On final note for those running scripts on SQL 2005 its a must that you start the script with :on error exit. This will avoid the problem detailed above, where one statement fails and but all the others are still run




Simon Sabin
SQL Server MVP

http://sqlblogcasts.com/blogs/simons
Post #260424
Posted Tuesday, February 21, 2006 9:20 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 18, 2009 1:00 AM
Points: 27, Visits: 9

I work in IST time zone. Now I am back to work.

The intention in the article is clear, to highlight the unknown error/issues and its outcome with absence/usage of GO statement.

Thanks guys for your valuable posts.

Cheers,

Krishnamurthy

 




Mani
Post #260472
Posted Tuesday, February 21, 2006 10:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Sunday, December 23, 2012 10:14 PM
Points: 4, Visits: 117

All you have to do is check out Books Online

GO

Signals the end of a batch of Transact-SQL statements to the Microsoft® SQL Server™ utilities.

Syntax

GO

Remarks

GO is not a Transact-SQL statement; it is a command recognized by the osql and isql utilities and SQL Query Analyzer.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO. SQL Query Analyzer and the osql and isql command prompt utilities implement GO differently.

Post #260478
Posted Wednesday, February 22, 2006 1:35 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Monday, August 5, 2013 1:19 AM
Points: 245, Visits: 13
hi, the behavior you experience is absolutely by design and no mystery. if you take a close look at BOL, you'll recognize that stored procedures NEVER required BEGIN and END statements. this is just a strange habit that some people seem to have. a stored procedure always starts with CREATE PROC and ends with GO (and that's the reason why it has to be the only statement in a batch).


Post #260508
Posted Wednesday, February 22, 2006 3:44 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 23, 2007 10:30 PM
Points: 3, Visits: 1

Of Course you do -- If you are going to script in multiple SPs in the same .SQL file. Also if you are going to script in a SP and a grant it some default permissions in the same .SQL file. Also if are going to create an SP and say 'print' out some status messages in the same .SQL file.

We have faced issues issues like this in our system (using VB and SQL2000, with all business logic implemented as SPs). The author is not saying the GO executes anything in the SP, but how a misplaced GO introduces unintentional behaviour into your SP.

M.

Post #260542
Posted Wednesday, February 22, 2006 8:55 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, July 27, 2010 7:51 PM
Points: 1,244, Visits: 3
cneuhold,

Thanks for clarifying. It really seems to me that the T-SQL language is not mature and can't match other compiled languages or PL/SQL.



Post #260641
Posted Thursday, February 23, 2006 1:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Friday, May 15, 2009 11:08 AM
Points: 1, Visits: 16
I don't know why people post such a bad article, It is waste of time and confuse the new people learning the technology. I request the authour to withdraw this article immediately.
Post #261135
Posted Friday, September 4, 2009 9:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, September 16, 2009 8:48 AM
Points: 2, Visits: 43
Suppose I want to eg. create a view part way through my stored proc. SQL Server will tell me "'CREATE VIEW' must be the first statement in a query batch." In a plain SQL I'd just throw in a GO to terminate the batch before the CREATE VIEW and start a new one.

Seems to me that's a reason for wanting to be able to use GO in a stored proc. Anyone know what to do about this?
Post #782994
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse