June 17, 2008 at 6:28 am
Do I even need it? I have seen it used in some example code from my course but I don't understand why it's used... could someone please explain its use?
Cheers.
June 17, 2008 at 6:34 am
GO is a batch delmiter. It shows where 1 batch ends and another begins. There are some SQL statements (CREATE PROCEDURE) that have to be the first statement in the batch.
One thing to bear in mind is that GO is not a SQL command. It's a command recognised by the SQL client tools (management studio, sqlcmd, etc)
Books online
SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of 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.Applications based on the ODBC or OLE DB APIs receive a syntax error if they try to execute a GO command. The SQL Server utilities never send a GO command to the server.
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
June 17, 2008 at 6:40 am
Hey thanks for that. Yeah I've read that on MSDN but I still didn't understand it. So I don't really have to use GO? It's not like transact sql or anything?
June 17, 2008 at 6:47 am
It's not T-SQL. You don't ever have to use it. There are many places it's convenient to use.
As I mentioned, there are statements that need to be the 1st in a batch.
This will give a syntax error - 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
SELECT GETDATE ()
CREATE PROCEDURE DoSomething AS
SELECT GETDATE()
EXEC DoSomething
This works, because the GO breaks the statements into 3 batches. 1 that selects the date, one that creates the procedure and one that execs the procedure
SELECT GETDATE ()
GO
CREATE PROCEDURE DoSomething AS
SELECT GETDATE()
GO -- ends the create procedure
EXEC DoSomething
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
June 17, 2008 at 6:57 am
Thanks heaps for that. 🙂
June 17, 2008 at 7:30 am
I often use GO between logical parts of a long script, to help me keep track of which parts are which. Even if it's not technically necessary, it can help break something up for better readability.
GO can also be used to execute a script multiple times.
select 1
go 10
Will select "1" 10 times (10 separate result sets). Not terribly useful with just a Select command, but can be useful with a proc or function you want to run a bunch of times for some reason, perhaps for performance testing or block/deadlock testing.
- Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
Property of The Thread
"Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon
June 17, 2008 at 7:54 am
Oh, ok. Don't think I'll be needing that just yet haha.
June 19, 2008 at 12:57 am
I am using GO to separate statements in long scripts and then my C# application is able to display how much (%) of the script is already processed.
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply