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
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.
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.
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.
GO signals the end of a batch of Transact-SQL statements to the SQL Server utilities.
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 lastGO
, or since the start of the ad-hoc session or script if this is the firstGO
.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 aGO
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.).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With