Truncate transaction rollback operation method in SQL Server _mssql2005

Source: Internet
Author: User
Tags rollback

We generally think that truncate is an operation that cannot be rolled back, it deletes all the data in the table and resets the identity column.

If you do a truncate operation in a transaction, you can roll back. Conversely, it does not recover data from the log file file. It does not record the deleted data in the log file, it only records the cell allocation of the data page in the log.

The following example will explain what is said above.

Copy Code code as follows:
Use Temp_test_database
Go
--Create a temporary table
CREATE TABLE Truncatetabel (ID INT)
INSERT into Truncatetabel (ID)
SELECT 1
UNION All
SELECT 2
UNION All
SELECT 3
Go

Copy Code code as follows:
--Check the inserted data
SELECT * from Truncatetabel

As shown in figure:

Start execution of a transaction

Copy Code code as follows:

--Start a transaction
BEGIN TRAN
TRUNCATE TABLE Truncatetabel
Go
--check truncatetable before rolling back
SELECT * from Truncatetabel
Go

F5 execution, as shown in figure:

To perform a ROLLBACK transaction

Copy Code code as follows:
--Rolling back transactions
ROLLBACK TRAN
Go

Check the table again truncatetable

Copy Code code as follows:
--check truncatetable again after rolling back
SELECT * from Truncatetabel
Go

F5 execution, as shown in figure:

To sum up, a transaction can be rolled back to a truncate operation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.