Solved

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

Posted on 2004-09-22
3
275 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now