Using BEGIN and END in SQL SERVER

Hi Experts,
What is the difference between using BEGIN TRANSACTION and END TRANSACTION, and using BEGIN and END?
When do we use one over the other?

Thanks in advance,
mrotor
mainrotorAsked:
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.

Lee SavidgeCommented:
Begin and End delimit a block of SQL statements, for example:

if @myval = 1
begin
 update table...
 insert into ...
end

Open in new window


See here for more: http://msdn.microsoft.com/en-gb/library/ms190487%28v=sql.100%29.aspx


If you didn't have the begin and end there it would only do the first statement as part of the IF statement and the insert would always happen.

Begin transaction allow you to wrap up a series of statements that are held in a transaction so that you can test for error conditions and if one is found you can rollback a transaction. If not you can commit it and it will physically make the changes in the data.

There isn't an "end transaction" because you either commit or rollback

http://msdn.microsoft.com/en-gb/library/ms188929%28v=sql.100%29.aspx
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
Priya SudharsanProgrammer AnalystCommented:
You need transations when you want to do more than one things, either all of them or none.

Begin and End are used to group the statements as logical. They may used in IF WHILE and CASE statements.

You can simply use BEGIN and END without any of the above statements.

Its just for a readability. They just group the statements and you would be able to see the steps clearly. Thats all.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
BEGIN and END are used to define a block of SQL statements depending of the logical of your code (Condition block, Loop block, Stored Procedure block, ...).

BEGIN TRANSACTION and END TRANSACTION are used to define a block of operations (INSERT, DELETE, UPDATE, SELECT, ...) to act as one and being committed (if everything's ok) or rollback (if something goes wrongs) so you can maintain the integrity of your database.
0
mainrotorAuthor Commented:
I will try some of your suggestion and get back to you all. Thanks
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
mainrotor - How's it going?  Looks like you asked us a softball question that multiple experts have answered..
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.