Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rollback a delete or truncate statements. 1

Status
Not open for further replies.

jsql12

Technical User
Mar 27, 2007
28
US
Hi there,

I'd like to know how to rollback:
1- Delete from table
2- Truncate table

Your help is greatly appreciated


 
I didn't think you could rollback a truncate statement? I was under the impression that it is not logged the same way as a delete would be logged in the TLOg.

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
sure you can

Code:
create table tableb (id int)
insert tableb values(1)

select *,'before tran' from tableb
begin tran a

truncate table tableb
select *,'after truncate' from tableb
rollback tran a 

select *,'after tran' from tableb

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
Google Interview Questions





 
Thanks for that Denis. I always thought you couldn't do that.

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
a truncate is still mimimally logged what that means is that the pointers to the pages are dealocated

IF you have a lool like lumigent can you re-create or undo the truncate?

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
don't know

yes I do

Code:
Recover data for a table that was accidentally dropped or truncated. Whoa, this is powerful stuff. Log Explore can recover tables that you have accidentally or purposely obliterated. Not only can tables be recovered from the log, but from SQL Server backups. This is a feature that even SQL Server does not provide. As you can see, Log Explorer is a powerful data recovery tool that can save your a lot of time.

read more here:
Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
Google Interview Questions
 
Thanks for your quick responses.
The situation is when the table was deleted/truncated, the statements were not inside a transaction x. The person that deleted or truncated the table did so by issuing in the query analyzer the following statements:
Delete from Table a
Truncate Table a
(or worse) Drop Table a
I don't know how to rollback these statements. Is there any way other than restoring from a backup.
Thanks
 
yes,
1.Save the transaction log now. Backup it up!
2. get lumigent log explorer.

- Paul [batman]
- If at first you don't succeed, find out if the loser gets anything.
 
Denis,

Wait one min...Next you'll tell me there's no Santa Clause. Since when can you rollback a truncate? How does this work? The reason I'm asking is because our so called IS team is have some issues with the trans logs and requesting all deletes be change to truncate where acceptable to lessen the load on the logs.

Well Done is better than well said
- Ben Franklin
 
I guess I'll just backup the log and do a restore from the previous backup I have.

Thanks for he help.
 
>>Since when can you rollback a truncate?

always within a tran, run this

Code:
create table tableb (id int)
insert tableb values(1)

select *,'before tran' from tableb
begin tran a

truncate table tableb
select *,'after truncate' from tableb
rollback tran a 

select *,'after tran' from tableb

it is true that a truncate will be much faster because the actual delete statement is not logged just where the row is located. That is the reson the log doesn't grow as much

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
Google Interview Questions





 
Thanks

I should of known that, because if I put a trunc in a nested trans and rollback the outer most trans I can undo the change. I wasn't thinking.

Well Done is better than well said
- Ben Franklin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top