tags:

views:

353

answers:

4

Is there a difference between "GO" and "BEGIN...END" in SQL Scripts/Stored Procedures? More specifically, does BEGIN...END specify batches just as GO does?

+11  A: 

GO is not actually a command understood by the server. It is simply a delimiter used by the client tool, e.g. Query Analyzer, to split the SQL up into batches. Each batch is then normally sent to the server separately. The client tool usually lets you configure the batch separator to be whatever you choose, GO is a convention.

BEGIN/END is a block marker which wraps a section of code in the same way that curly braces do in other languages.

Anonymous Coward
+2  A: 

GO

Separates different statements in batchs (you can't use it inside a Stored procedure)

BEGIN .. END

Is used to group things inside a SP, like { and } in C#

IF x
BEGIN 
   -- do y
   -- do w
END
Eduardo Molteni
Actually you can use it in a stored proc but nothing past the GO will be executed which is why you don't want to use it in a stored proc.
HLGEM
+1  A: 

It is also a good practice to use begin and end statements anytime you use an if in t-sql. If you do not use a begin and end, then only the next statement after the if statement will be executed as part of the if and this often leads to bugs.

HLGEM
+1  A: 

One more little thing about GO.

Variables defined between GOs are only scoped to that region and do not exist outside that region.

wcm