?
Solved

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

Posted on 2004-09-22
3
Medium Priority
?
287 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 1500 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

831 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