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 Wednesday, February 01, 2006 9:16 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, February 21, 2006 6:02 AM
Points: 33, Visits: 1
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/sKrishnamurthy/thedodgygostatement.asp


Kindest Regards,

Mani

Post #254975
Posted Tuesday, February 21, 2006 12:10 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 25, 2010 1:37 AM
Points: 345, Visits: 19

The author seems to think that begin and end statements seem to do something in stored procedures where they do not.

You can put a begin and end statement around whatever you like, but it wont stop the batch from continuing on to what comes next.

You also don't even need a GO statement in order to create a stored procedure. The authors seems to have totally missed the purpose of the GO statement.

Did you know, that SQL Server doesn't even understand a GO statement. It is not transact SQL!

Hope this helps.

Post #260070
Posted Tuesday, February 21, 2006 2:00 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Wednesday, November 28, 2012 9:56 AM
Points: 82, Visits: 154

Do you really understand what happens when you create a stored procedure?

Interesting question!  I agree with the above poster.  The BEGIN and END statements are nothing to do with stored procedures but are simply used to logically group statements in IF's/ELSE's, WHILE's and CASES's.

Post #260090
Posted Tuesday, February 21, 2006 6:30 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Today @ 5:16 AM
Points: 73, Visits: 73

With you there.  There's more to GO than meets the eye, and it doesn't even have to be "GO" either!

Thort it might be fun to share this early tale of GO horror (I was a lot younger then):

SQL 7, asked to implement a script from a (VB) developer to add a column to a table on a remote, client mission-critical, really, really, really important customer address db. 

So, did I double-check the code?  No. 

Did I test it on a local db?  No. 

Can you see a disaster coming?  Oh, yes...

Run the script.  See the script run.  See the error.

Turns out VB Fly Boy had scripted the change from Enterprise Mangler and it went something like this:

SELECT * INTO newtable FROM oldtable

GO

DROP TABLE oldtable

GO

CREATE TABLE oldtable (with shiny new column)

GO

INSERT oldtable SELECT * FROM newtable

GO

Okey-dokey, except the first SELECT INTO failed.  But, thanks to the joys of "GO" the script ploughed on, dropped the table etc.  So, ended up with the new table with its new column and no data!

Oops.  Well, that'd be ok 'cos the local dba on the ground will have a back up.  Well, yes, from 3 months ago.

Lots and lots of lessons learned there.  I now spend my days doing macrame.




Post #260155
Posted Tuesday, February 21, 2006 6:41 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
I wonder if some of the confusion is from people transitioning from Oracle PL/SQL and other compiled languages where the language is much more sophisticated and clear.

CREATE/BEGIN/END have clear syntactical and semantic roles in PL/SQL. In fact, the END statement includes the name of the procedure or function that is being defined.

SUB/END SUB and FUNCTION/END FUNCTION in VB.NET are also very clear.

T-SQL still seems like a toy language compared to others for this and some other reasons.

--Peter



Post #260161
Posted Tuesday, February 21, 2006 7:24 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
"GO" is not a real SQL statement. It's a batch termination indicator for Query Analyzer. You might want to point out that trying to use "GO" in your scripts outside of Query Analyzer (like in a .NET SqlCommand), you can expect to catch some exceptions.
Post #260188
Posted Tuesday, February 21, 2006 7:37 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 25, 2010 1:37 AM
Points: 345, Visits: 19

I think I already said that GO wasn't a real sql statement in my earlier message.

Are we surprised that we haven't heard from the author?

Post #260199
Posted Tuesday, February 21, 2006 8:59 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, August 31, 2013 11:41 PM
Points: 121, Visits: 34
I am having real trouble understanding where I would use this 'approach' in real-world scenarios. In my experience with SQL Server (almost 2 years now) I have not come across one situation where the GO keyword caused me trouble. I use the SQLIDE Pro from Imceda/QUEST and the only time it uses the GO keyword is to do


SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


at the start and

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

at the end: I never had to use the GO keyword within a stored procedure.
Maybe I am missing something here
Post #260252
Posted Tuesday, February 21, 2006 9:04 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Thursday, March 25, 2010 1:37 AM
Points: 345, Visits: 19

No, you're not missing anything. You don't have to use the GO word when creating stored procs.

 

Post #260258
Posted Tuesday, February 21, 2006 9:06 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, August 31, 2013 11:41 PM
Points: 121, Visits: 34

Colin, thanks for the backup.

That was exactly my point: You can live without GO as far as creating Stored Procedures are concerned (this being the point discussed in the article)
Post #260260
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse