What is the benefit of using BEGIN and END in SQL?

What is the benefit of using BEGIN and END in SQL?


Declare @p1 int
set @p1 = 1
BEGIN
IF @p1 = 12
print @p1
ELSE
print 'hello world'
END

print 'hello world2'
Mr_ShawAsked:
Who is Participating?
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.

Carl TawnSystems and Integration DeveloperCommented:
BEGIN and END create a code block, a way of grouping multiple statements. Normally you would use them in conjunction with some sort of looping of decision (IF...ELSE, etc) construct.
0

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
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
It allows you to group statements and to be executed as group.


Declare @p1 int
set @p1 = 1

IF @p1 = 1
      print (@p1)
print(@p1*2)

and also try

Declare @p1 int
set @p1 = 1

IF @p1 = 1
BEGIN
      print (@p1)
      print(@p1*2)
END
0
Lee SavidgeCommented:
It is there to delimit the block of code. If you have an if/else statement where there if and else blocks only have a single line of code you don't need the begin and end but I personally put them in to make it clear that the block is there.

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
0
cyberkiwiCommented:
If you need to use it, then you need to use it. It is not a benefit. Begin-End is used to logically block several statements together, such as shown in comments above.

When you don't need it, then we can talk about "benefit".  In this case, the "benefit" is purely decorational, and personally I don't believe they add any real value to a statement batch.
0
Asim NazirCommented:
Hi,

Basically it marks a Batch. A batch is a group of one or more Transact-SQL statements sent at the same time from an application to SQL Server for execution. SQL Server compiles the statements of a batch into a single executable unit, called an execution plan. The statements in the execution plan are then executed one at a time.
Details of a batch: http://msdn.microsoft.com/en-us/library/ms175502.aspx

0
Asim NazirCommented:
Although all Transact-SQL statements are valid within a BEGIN...END block, certain Transact-SQL statements should not be grouped together within the same batch, or statement block.
Details: http://msdn.microsoft.com/en-us/library/ms190487.aspx
0
Mr_ShawAuthor Commented:
thanks
0
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 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.