Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between GO and BEGIN...END

Tags:

sql-server

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?

like image 759
Mark Carpenter Avatar asked Dec 30 '08 19:12

Mark Carpenter


People also ask

When to use begin and end?

BEGIN and END are used in Transact-SQL to group a set of statements into a single compound statement, so that control statements such as IF … ELSE, which affect the performance of only a single SQL statement, can affect the performance of the whole group.

Do I need begin end in stored procedure?

END at the start and end of a stored procedure and function. But it is not strictly necessary. However, the BEGIN... END is required for the IF ELSE statements, WHILE statements, etc., where you need to wrap multiple statements.

What is begin in SQL?

Begin SQL is a keyword used in the Method editor to indicate the beginning of a sequence of SQL commands that must be interpreted by the current data source of the process (the integrated SQL engine of 4D or any source specified via the SQL LOGIN command).

Why set Nocount on is used in SQL?

SET NOCOUNT ON prevents the sending of DONEINPROC messages to the client for each statement in a stored procedure.


2 Answers

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.

like image 150
Anonymous Coward Avatar answered Sep 23 '22 13:09

Anonymous Coward


One more little thing about GO.

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

like image 28
wcm Avatar answered Sep 24 '22 13:09

wcm