Solved

Why a single update, delete and insert operations are atomic, even without using Begin Transaction ?

Posted on 2004-09-22
3
281 Views
Last Modified: 2012-06-27
Hi,

I have a doubt about the internal behavior of how SQL Server handle errors in update/delete/insert operations.

If say, I have an "Update" statement, that will update 10000 records and in the middle of the update, some error happens, like a check constraint violation, I review the table and non of the records where updated. That's very nice, right ? Even without adding a Begin Transaction / Update|Delete|Insert ... / Commit transaction, the operation is always atomic.
So the question is how does it do it ?

I have some guess. First, could it be starting an internal/hidden transaction  ? If so, is that transaction the same kind of transaction we use in our code or do they have their own special internal transaction to handle this ?
Or, if this is not a transaction at all, then what is it ?

Thanks in advance,



0
Comment
Question by:fischermx
  • 2
3 Comments
 
LVL 15

Expert Comment

by:jdlambert1
ID: 12125090
Your guess is correct, and it's called an "implicit" transaction. It's the same kind of transaction as when you write BEGIN TRAN in T-SQL.
0
 
LVL 1

Author Comment

by:fischermx
ID: 12125265

Thanks for your answer,  jdlambert1.

However, let me clarify this for other readers. The term "Implicit Transaction" has slightly different meaning. It is when you never have to call "begin transaction", you just say commit or rollback. You can do some insertions, then some udpates and so on, and then, you just call commit and all of them are commited.
Each next transaction begins exactly where the prior ended, without having to begin it explicitly, that's why they're called implicit :)

I understand that what I'm seeing is a sort-of implicit transaction, since it looks like a transaction and behaves like a transaction and since I didn't started, then it is "implicit" as you say. But I wanted clarify the term.

So, my question was what happens inside a SINGLE update statement that handles hundreds of records and it fails in the middle of it. I guess I may find a deeper anwser in Kalen Delaney's SQL Server book, which is on the way from amazon, but I wanted to gather some opinions here in the meantime.

Regards,

0
 
LVL 15

Accepted Solution

by:
jdlambert1 earned 500 total points
ID: 12125396
It's the same meaning to my way of thinking: a transaction must be explicit or implicit, and if your code doesn't state the beginning of the transaction, that's implicit. All the major RDBMS's use transactions for all single statements by design, for the sake of data integrity, obviously. If you want independent behavior, you have to put each record (or group of records you want done as a unit) in separate statements.

Inside SQL Server is a great book, and I hope they get Kalen to update it again for 2005.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question