Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should we end stored procedures with the GO statement?

Should we end stored procedures with GO statement, if so what are the advantages of using GO?

CREATE PROCEDURE uspGetAddress @City nvarchar(30)
AS
SELECT * 
FROM AdventureWorks.Person.Address
WHERE City = @City
GO
like image 244
Omer Avatar asked Jun 09 '14 19:06

Omer


People also ask

How do you end a stored procedure?

The answer is simple, the end of the statement batch. Even if you are using a "BEGIN ... END" statement block, the stored procedure structure is not going to end at the end of the statement block. It will continue to the end of the statement batch, usually the GO command.

Can you use go in a stored procedure?

In SSMS, the GO command is very useful if you have several sentences to execute. For example, you cannot create several stored procedures without the GO command. The error message will be Incorrect Syntax. The CREATE PROCEDURE must be the only statement in the batch.

When to use begin and end in stored procedure?

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.

What is the purpose of Go statement in SQL?

GO signals the end of a batch of Transact-SQL statements to the SQL Server utilities.


1 Answers

The statement go, per the documentation

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

...

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.

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.

A Transact-SQL statement cannot occupy the same line as a GO command. However, the line can contain comments.

Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword. The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO command.

A stored procedure definition, per the documentation for create procedure, comes with restrictions. it must be the first (and only) statement in the batch:

The CREATE PROCEDURE statement cannot be combined with other Transact-SQL statements in a single batch.

That means the body of stored procedure ends with the batch. Adding GO in your source file is good practice. Especially since it's common to do things prior to and following the creation of a stored procedure. You'll often see source files that look something like this:

if (object_id('dbo.foobar') is not null ) drop procedure dbo.foobar
GO
-- dbo.foobar --------------------------------------------
-- 
-- This stored procedure does amazing and wonderful things
----------------------------------------------------------
create procedure dbo.foobar
as

   ...
   {a sequence of amazing and wonderful SQL statements}
   ...
   return 0
GO

grant execute on dbo.foobar to some_schema
GO

And the value for GO is adjustable in Sql Server Management Studio's options. If you'd like to use something like jump instead of go, you can (bearing in mind that you're almost certainly going to give yourself grief in doing so.).

options screenshot

like image 82
Nicholas Carey Avatar answered Oct 11 '22 18:10

Nicholas Carey