Sie sind auf Seite 1von 8

SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

Home
Resume
Contact Me
All Articles
Disclaimer
Jobs
Search

Journey to SQL Authority with Pinal Dave


Notes of a SQL Server Database Administrator
Feed on
Posts
Comments

SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files After Transaction
Session Is Closed
December 26, 2007 by pinaldave

You might have listened and read either of following sentence many many times.

“DELETE can be rolled back and TRUNCATE can not be rolled back”.

OR

“DELETE can be rolled back as well as TRUNCATE can be rolled back”.

As soon as above sentence is completed, someone will object it saying either TRUNCATE can be or can not be rolled back. Let us make sure
that we understand this today, in simple words without talking about theory in depth.

While database is in full recovery mode, it can rollback any changes done by DELETE using Log files. TRUNCATE can not be rolled
back using log files in full recovery mode.

DELETE and TRUNCATE both can be rolled back when surrounded by TRANSACTION if the current session is not closed. If
TRUNCATE is written in Query Editor surrounded by TRANSACTION and if session is closed, it can not be rolled back but DELETE
can be rolled back.

Let us understand this concept in detail.

In case of DELETE, SQL Server removes all the rows from table and records them in Log file in case it is needed to rollback in future. Due
to that reason it is slow.

In case of TRUNCATE, SQL Server deallocates the data files in the table and records deallocation of the data files in the log files. If
deallocated data files are overwritten by other data it can be recovered using rollback. There is no guarantee of the rollback in case of
TRUNCATE. However, while using T-SQL following code demonstrates that TRUNCATE can be rolled back for that particular session.

First create test table which some data. Afterwards run following T-SQL code in Query Editor and test the effect of TRUNCATE on created
test table.
BEGIN TRAN
TRUNCATE TABLE TestTable
-- Following SELECT will return TestTable empty
SELECT * FROM TestTable
-- Following SELECT will return TestTable with original data
ROLLBACK
SELECT * FROM TestTable

Summary : DELETE can be recovered from log file always if full recovery mode is set for database. TRUNCATE may or may not be
recovered always from log files.

Reference : Pinal Dave (http://www.SQLAuthority.com)

Posted in Author Pinal, SQL, SQL Authority, SQL Query, SQL Scripts, SQL Server, SQL Tips and Tricks, T SQL, Technology | 11

1 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

Comments

11 Responses to “SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files After Transaction Session
Is Closed”

1. on December 27, 2007 at 2:51 pm1 kris

i have table with the data like below:

ids_cloumn
========================
1 , 2 , 4 , 23 , 12 , 11 , 9 , 8 , 7 , 10 ,
1 , 3 , 4 , 21 , 16 , 12 , 19 , 10 ,
2 , 3 , 6 , 24 , 15 , 18 , 9 , 8 ,
3 , 5 , 8 , 27 , 14 , 19 , 39 , 7 , 13 ,
1 , 7 , 9 , 28 , 19 , 11 , 29 , 8 ,

i am having a list of nos with me (coming from another query)


ex1: 1, 3 & 23
ex2 : 1, 6, 34 & 23

now i want to retrieve the list of rows which is having any of the nos.

is there any other way with out using “LIKE %”

If i am using like i need to write so many OR conditions based on the inputs.

thanks

2. on December 29, 2007 at 7:39 am2 Andrew Chen

The ping back doesn’t seem to work. Anyway I don’t think a commited delete statement can be rolled back either. The deleted data
can be restored from backup only.

3. on December 29, 2007 at 9:10 am3 pinaldave

Hi Andrew,

Thanks for comment. I have tested it earlier and delete can be rolled back using log files even though it is committed. You will have to
use point of time restore using log files. However, Truncate can not be restored using log files. I tried to convey this message in title of
this post.

I appreciate your regular participation in this blog. Your comments are always valuable.

Regards,
Pinal Dave ( http://www.SQLAuthority.com )

4. on December 29, 2007 at 5:50 pm4 Andrew Chen

Hi Pinal,

Thanks for replying. I feel confuse when you mention roll back using log file together with the “Roll Back” statement. Roll back
statement only rolls back the current open transaction in current database session. It can not roll back a committed transaction.

Roll back using log file however will roll back many transactions. we can not roll back a specific delete that way. Everthing done after
the delete will also get rolled back. I got the impression from your post that we can roll back a specific delete statement using log file.

2 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

As you point out roll back using log file is to restore a database to a point of time

5. on December 29, 2007 at 6:27 pm5 Andrew Chen

I forgot to mention that I think point in time restore using log file can also roll back “TRUNCATE”.

6. on December 29, 2007 at 6:37 pm6 pinaldave

Andrew,

Point in time restore using log file can not roll back TRUNCATE if the data file which is deallocate is over-written. That is the
difference.

That is why I used last statement in post “TRUNCATE may or may not be recovered always from log files.”

Kind Regards,
Pinal Dave ( http://www.SQLAuthority.com)

7. on December 30, 2007 at 3:27 am7 Andrew Chen

Pinal,

Sorry for being stubborn. But as far as I know in a point in time restore. A full database backup has to be be restored first and then the
log backup is actually used to “Roll Forward” the database to the point in time when the data deleted or turncated were still exist. So it
would not be a problem whether or not the data file got over written because it will be re-created in database restore anyway.

Maybe I miss unstand something but if you can share an example of using log file to “Roll Back” a commited DELETE then it can
make things clear. I believe it would be something a lot of DBAs like know about

Appreciated
Andrew

8. on January 1, 2008 at 11:36 pm8 Alexandr Volok

I’m completely agree with Andrew. Truncate table not just safe for rollback in transaction scope. It still possible undo truncation of
table using restore log and stopat.

There just reproduction code:

– STEP 1. Creating DB
SET NOCOUNT ON
CREATE DATABASE TEST
GO
ALTER DATABASE TEST SET RECOVERY FULL
USE TEST
GO

— STEP 2. Creating table with some data (100 rows)


CREATE TABLE data (f1 INT IDENTITY)
GO
INSERT data DEFAULT VALUES
GO 100

– STEP 3. Performing full backup


BACKUP DATABASE TEST TO DISK=’c:\test_full.bak’ WITH INIT
GO

3 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

– STEP 4. Performing log backup


DECLARE @date DATETIME

– variant1: Date mark just before truncation


SET @date = DATEADD(s, 0, GETDATE())

– variant2: Date mark after truncation


– SET @date = DATEADD(s, 1, GETDATE())

TRUNCATE TABLE data


WAITFOR DELAY ‘00:00:03′
BACKUP LOG TEST TO DISK=’c:\test_log.bak’ WITH INIT

– STEP 5. Restoring Test DB.


USE MASTER
RESTORE DATABASE TEST FROM DISK=’c:\test_full.bak’ WITH NORECOVERY, REPLACE
RESTORE LOG TEST FROM DISK=’c:\test_log.bak’ WITH RECOVERY, STOPAT=@date
GO
USE TEST
GO
select COUNT(*) AS CNT FROM data
go
USE MASTER
DROP DATABASE TEST

p.s. Please, delete my previous comment :))

9. on January 2, 2008 at 12:26 pm9 pinaldave

Hi Alexandr Volok and Andrew,

Both of yours comment makes this article very valuable. Please continue writing your opinion and comments.

Kind Regards,
Pinal Dave ( http://www.SQLAuthority.com )

10. on January 6, 2008 at 8:12 am10 Andrew Chen

Acutally there are third party transaction Log reading Tools that are able to read transaction logs and allow you undone commited
statements or transactions. Tuncate satement may or may not be undone using such tools. SQL server itselft does not provide this
capability.

Siusic Dot Com

11. on January 6, 2008 at 10:29 am11 pinaldave

Hello Andrew,

Thank you for posting clarification and as well we are in agreement. I respect your additional research as well writing about this on
your blog http://www.siusic.com/wphchen/recover-data-using-transaction-log-144.html

You are always welcome to comment and participate here to help community.

Kind Regards,
Pinal Dave ( http://www.SQLAuthority.com )

Trackback URI | Comments RSS

4 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

Leave a Reply

Name (required)

Mail (will not be published) (required)

Website

Submit Comment

About Pinal Dave


Pinalkumar Dave is the author of over 400 SQL Server articles. He has five years experience as Principal Database Administrator in
MS SQL Server 2005/2000 and ColdFusion MX. He has a Masters of Science degree in Computer Networks, along with MCDBA,
MCAD(.NET) and ColdFusion Advanced MX Certifications.

Blog Stats
1,760,293 Readers

My Links
My Homepage
My Resume
My Other Blog
SQL Interview Q & A
SQL Coding Standards
SQL Random Article
SQLAuthority BEST Articles
>> Search SQLAuthority <<
Subscribe Email Update
SQLAuthority Feed
iGoogle Gadget
-------
Find Post SQL Jobs

Top Posts
SQL Server Interview Questions and Answers Complete List Download
SQL SERVER - Insert Data From One Table to Another Table - INSERT INTO SELECT - SELECT INTO TABLE
SQL SERVER - 2005 - Create Script to Copy Database Schema and All The Objects - Stored Procedure, Functions, Triggers,
Tables, Views, Constraints and All Other Database Objects
SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
SQL Server Interview Questions and Answers - Introduction
SQL Server Interview Questions and Answers - Part 1

5 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

SQL SERVER - Fix : Error : 40 - could not open a connection to SQL server.
SQL SERVER - 2005 NorthWind Database or AdventureWorks Database - Samples Databases
SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
SQL Server Interview Questions and Answers - Part 2
SQL Server Interview Questions and Answers - Part 6
SQL SERVER - Restore Database Backup using SQL Script (T-SQL)

Categories
About Me (19)
Author Pinal (327)
Best Practices (10)
Data Warehousing (16)
Database (127)
DBA (155)
Main (62)
Software Development (41)
SQL (477)
SQL Add-On (45)
SQL Authority (477)
SQL Backup and Restore (38)
SQL Coding Standards (76)
SQL Constraint and Keys (26)
SQL Cursor (54)
SQL Data Storage (3)
SQL DateTime (21)
SQL Documentation (217)
SQL Download (357)
SQL Error Messages (339)
SQL Function (72)
SQL Humor (15)
SQL Index (40)
SQL Interview Questions and Answers (41)
SQL Joins (332)
SQL Performance (344)
SQL Query (477)
SQL Scripts (380)
SQL Security (332)
SQL Server (477)
SQL Server DBCC (242)
SQL Stored Procedure (101)
SQL Tips and Tricks (477)
SQL Trigger (33)
SQL Utility (24)
SQLAuthority (3)
SQLAuthority Author Visit (15)
SQLAuthority Book Review (26)
SQLAuthority News (64)
SQLAuthority Website Review (12)
T SQL (477)
Technology (477)

Recent Posts
SQL SERVER - Introduction to Performance Monitor - How to Use Perfmon
SQL SERVER - Introduction to Three Important Performance Counters
SQL SERVER - Get Current Database Name
SQL SERVER - 2005 - Find Unused Indexes of Current Database
SQLAuthority News - RIP: Ken Henderson, 1967 - 2008
SQLAuthority News - 2008 - Download - SQL Server 2008 Brochure
SQL SERVER - Microsoft SQL Server Compact 3.5 SP1 Beta for ADO.Net Entity Framework Beta 3
SQL SERVER - Sharpen Your Basic SQL Server Skills - Database backup demystified
SQL SERVER - Import CSV File Into SQL Server Using Bulk Insert - Load Comma Delimited File Into SQL Server
SQLAuthority News - SQL Joke, SQL Humor, SQL Laugh - Funny Microsoft Quotes

6 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

SQL SERVER - Simple Example of WHILE Loop with BREAK and CONTINUE
SQL SERVER - FIX : ERROR : Cannot find template file for new query (C:\Program Files\Microsoft SQL Server\90\Tools\
Binn\VSShell\Common7\ IDE\sqlworkbenchprojectitems\Sql\ SQLFile.sql)
SQL SERVER - Find All The User Defined Functions (UDF) in a Database
SQL SERVER - Find Great Job with Great Pay
SQL SERVER - Top 10 Best Practices for SQL Server Maintenance for SAP

Recent Comments
senthilkumar.s on SQLAuthority News - 2008 - Download - SQL Server 2008 Brochure
Parmod Bhardwaj on SQL SERVER - Fix : Error 8101 An explicit value for the identity column in table can only be specified
when a column list is used and IDENTITY_INSERT is ON
Gayathri on SQL SERVER - Fix : Error : 40 - could not open a connection to SQL server.
Premsagar on SQL Server Interview Questions and Answers - Introduction
Vinay.R on SQL Server Interview Questions and Answers - Part 2
weixin on SQL SERVER - 2005 NorthWind Database or AdventureWorks Database - Samples Databases - Part 2
Jorjie on SQL SERVER - Insert Multiple Records Using One Insert Statement - Use of UNION ALL
igor on Contact Me
BADRI on SQL SERVER - Find Length of Text Field
SQL SERVER - Introduction to Three Important Performance Counters Journey to SQL Authority with Pinal Dave on SQL
SERVER - Introduction to Performance Monitor - How to Use Perfmon
SQL SERVER - Introduction to Performance Monitor - How to Use Perfmon Journey to SQL Authority with Pinal Dave on SQL
SERVER - Introduction to Three Important Performance Counters
ravi on Contact Me
Magnus on SQL SERVER - 2005 Take Off Line or Detach Database
Megha Raychand on Contact Me
Arulraj on SQL Server Interview Questions and Answers - Part 2

Archives
February 2008
January 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
February 2007
January 2007
December 2006
November 2006

December 2007
M T W T F S S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
« Nov Jan »

Pages

7 of 8 2/18/2008 7:16 PM
SQL SERVER - TRUNCATE Can’t be Rolled Back Using Log Files Aft... http://blog.sqlauthority.com/2007/12/26/sql-server-truncate-cant-be-rolle...

Resume
Contact Me
All Articles
Disclaimer
Jobs

Blog at WordPress.com. | Theme: Mistylook by Sadish.

8 of 8 2/18/2008 7:16 PM

Das könnte Ihnen auch gefallen