Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 when to use begin and end in stored procedure

Author  Topic 

learning_grsql
Posting Yak Master

230 Posts

Posted - 2012-08-13 : 09:01:34
first of all, i'm new to sql OR stored procedure.

while reading online examples, i notice that in some examples there is [begin] and [end] and in some there is nothing as below.


create procedure newprocedure
as
select * from table1
GO


with [begin] and [end]


create procedure newprocedure
as
BEGIN
select * from table1
END
GO


I wonder when we use [begin] and [end] and when we should not use them.

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-08-13 : 09:16:54
For stored procedures it is optional. But, in some other cases, it is mandatory - for example, for a scalar function you have to have the begin and end. If you use IF/ELSE constructs and if you have more than one statement in the IF block, you will need a begin and end in the if block etc.

In many cases, you can tell whether it is mandatory or not by examining the MSDN page and looking to see if the BEGIN/END are in square brackets - for example, here it is in square brackets:http://msdn.microsoft.com/en-us/library/ms187926.aspx
Here it is not (hence mandatory) http://msdn.microsoft.com/en-us/library/ms186755.aspx
Go to Top of Page
   

- Advertisement -