473,385 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

When should I to wrap DML to transaction

Hi to all.

Many times i saw that some people wrap the single insert, delete or update
statements to transaction.

My question is suppose i have procedure , and inside this procedure i perform
update i exatly know that only one row will be updated , is necessary or when
i need to wrap it with transaction

------------------------------------------------------------------------------

CREATE PROCEDURE Test_sp
@ID int

as

UPDATE MyTable
SET col1 = 'bla bla'
WHERE [ID] = @ID

------------------------------------------------------------------------------
-
in code above only on row will be updated so why some times i see :

------------------------------------------------------------------------------

CREATE PROCEDURE Test_sp
@ID int

as

BEGIN TRAN

UPDATE MyTable
SET col1 = 'bla bla'
WHERE [ID] = @ID

SELECT @rowcount = @@ROWCOUNT,
@error = @@ERROR

IF @error = 0 AND @rowcount =1
GOTO SUCS_STEP
ELSE
ROLLBACK TRANSACTION TRC_EVENT
GOTO ERROR_STEP

END

ERROR_STEP: RETURN 0
SUCS_STEP:
COMMIT TRANSACTION TRC_EVENT
RETURN 1
GO
------------------------------------------------------------------------------
-

The update statement is itself in transaction so why there are need to wrap
to tranasction.

So PLZ explain in wich cases i need to wrap (INSERT, UPDATE, DELETE)
statements into
transactions????
ONLY when i perform a single statement i.e one insert or one delete or one
update !!!!

TNX.

--
"Imagination is more important than knolwege" (Albert Einshtein)
Message posted via SQLMonster.com
http://www.sqlmonster.com/Uwe/Forums...neral/200508/1
Aug 22 '05 #1
1 1894
Without BEGIN TRAN, you can't do a ROLLBACK if something unexpected
happens. In your code sample above, the developer expects that the
UPDATE will affect only one row, but if that isn't what happens, he
wants to rollback the UPDATE - perhaps two rows were affected, perhaps
there was another error of some kind.

You might find this article useful for general information about error
handling:

http://www.sommarskog.se/error-handling-II.html

Simon

Aug 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John Mosey | last post by:
A huge (and never used) database log was taking up about 4 GB of HD space. We want the data for historical capacity, however, don't care about the transactions log. After a bit of research I ran...
13
by: Patrick | last post by:
I understand that with IIS5.1 on Windows XP Professional SP1, I can 1) Either set under IIS Manager-> Any specific Virtual Directory-> Configuration->Options->ASP Script timeout for all pages...
9
by: heruti | last post by:
Hi all... I've been stumped by this for days. Bit of ASP code: (IIS) Set LocalConn = CreateObject("ADODB.Connection") LocalConn.CursorLocation = adUseClient LocalConn.CommandTimeout = 0...
0
by: akej via SQLMonster.com | last post by:
Hi to all. Many times i saw that some people wrap the single insert, delete or update statements to transaction. My question is suppose i have procedure , and inside this procedure i perform...
4
by: graphicsxp | last post by:
Hello, I've got a few stored procedure called made within one sqlTransaction object. If the .Rollback() method of the transaction is called, then I would like some extra code to be executed....
0
by: cwho.work | last post by:
Hi! We are using apache ibatis with our MySQL 5.0 database (using innodb tables), in our web application running on Tomcat 5. Recently we started getting a number of errors relating to...
2
by: Martin Z | last post by:
I'm using the TableAdapterHelper to set the connection and transaction properties on all the commands of all my typed table adapters.... I've checked at the time of the error and all the commands...
5
by: Allan Ebdrup | last post by:
I'm using dotNet 2.0 and System.Transactions to run transactions that span multiple database queries, Now I would like to log any Sql errors that occur in the transaction, but when I insert the...
6
by: Tark Siala | last post by:
hi i'm using VS.NET 2005 +SP1 C#, and i connect access database by ADO.NET 2.0. i'm using Dataset to (insert,update,delete) and in get data i use (Datareader), but my problem when i do this: 1 -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.