Can we rollback the truncate statment and what is the difference?

Filed under: Transact-SQL (T-SQL) — KKline at 5:59 pm on Thursday, August 30, 2007

Q:  Can we rollback the truncate statment? whats the difference between delete and truncate?

Kevin Kline says:  You cannot rollback TRUNCATE. It is a “non-logged statement”. TRUNCATE removes ALL records from a table.

You -can- rollback DELETE. It is a “logged statement”. DELETE removes one or more records from a table according to the conditions of your WHERE clause.

My recommendation is not to use TRUNCATE in a production application since you cannot recover from it. Instead, use DELETE unless you have a very strong reason to use TRUNCATE.

Technorati Tags:
, , ,

1 Comment »

Comment by ivanrdgz

October 15, 2008 @ 10:20 pm

Run this script

create table t(dob datetime)
go

insert into t
select getdate()
go 10

select * from t

begin tran

truncate table t

select * from t

rollback

select * from t

You will see that the truncate indeed is roll backed.

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>