Is it necessary to include "BEGIN" and "END" statements in a SQL script?

Hi.  A programmer came in and helped us generate a lot of SQL code.  I'm a novice SQL user but I can follow along and decipher what steps are being performed so am reviewing to understand the general flow of the application better.  I noticed that in many places the programmer did not use BEGIN and END statements, while in other places, they are being used.  Is this just good syntax or are these statements required to perform some functions while others, not so much?  A couple of the Sprocs with these statements included are simply calling out table updates but same thing is happening in another sproc and the begin/end statement are not used.  thanks
LVL 7
valmaticAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian CroweDatabase AdministratorCommented:
A stored procedure does not require an outer BEGIN/END statement.  There is not harm in adding them to a stored procedure but they aren't necessary.  It comes down to local coding standards and personal preference really.

However, a user-defined function DOES require a BEGIN & END.

Within a procedure, say for an IF statement you must include a BEGIN/END if there is more than one statement associated with the conditional.  They are not necessary but good practice to always have a BEGIN/END even for a single statement.

i.e.

IF (@A = @B)
BEGIN
   -- do something
END

is equivalent to

IF (@A = @B)
   --do something

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jim HornMicrosoft SQL Server Data DudeCommented:
In addition to the above correct answer couple of other places too, such as cursors and WHILE loops, but not many.
Doug BishopDatabase DeveloperCommented:
No points please, but to expand on Brian's comment, the reason it is good practice around an IF statement is to make sure you don't break logic if another statement is subsequently added. It is clear that it belongs inside the IF block. If you start with:

IF (@A = @B)
   SET @B = @B + 1
SET @A = @A - 1

@B will be incremented only if @A and @B are equal and @A will ALWAYS be decremented. Using BEGIN/END makes it clear exactly what should be associated with the IF statement.
valmaticAuthor Commented:
Thanks so much for the quick responses.  Exactly what I wanted to know for my own growing knowledge.  
Bishop, I know you said no points but it's just good form man..  Plus your insight is great.  :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.