Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are BEGIN / END keywords required in a stored procedure? [duplicate]

Tags:

syntax

sql

tsql

Possible Duplicate:
When do I need to use Begin / End Blocks and the Go keyword in SQL Server?

Example:

CREATE PROCEDURE DoSomething
AS
BEGIN
    SET NOCOUNT ON;

    -- Insert lots of statements in here, including other stored procedures.
END

Do you need the BEGIN and END? Does it make ANY difference if you have them or not?

like image 211
JJ. Avatar asked Oct 02 '12 20:10

JJ.


2 Answers

They are optional.

From MSDN - CREATE PROCEDURE:

From the definition of the command -

AS { [ BEGIN ] sql_statement [;] [ ...n ] [ END ] }

And later on in the page:

{ [ BEGIN ] sql_statement [;] [ ...n ] [ END ] }

One or more Transact-SQL statements comprising the body of the procedure. You can use the optional BEGIN and END keywords to enclose the statements.

like image 91
Oded Avatar answered Nov 12 '22 00:11

Oded


As indicated in the CREATE PROCEDURE documentation, they are optional.

{ [ BEGIN ] sql_statement [;] [ ...n ] [ END ] }

One or more Transact-SQL statements comprising the body of the procedure. You can use the optional [emphasis added] BEGIN and END keywords to enclose the statements. For information, see the Best Practices, General Remarks, and Limitations and Restrictions sections that follow.

Personally, I always include them, but that's just me.

like image 31
Joe Stefanelli Avatar answered Nov 12 '22 02:11

Joe Stefanelli