SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 when to use begin and end in stored procedure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

learning_grsql
Posting Yak Master

155 Posts

Posted - 08/13/2012 :  09:01:34  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/13/2012 :  09:16:54  Show Profile  Reply with Quote
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
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000