Q. What is the difference between Truncate and Delete in SQL Server?The answer that, in my experience, is most expected and most often given is that Delete allows you roll back the data you have wiped, while Truncate does not - Truncate is irreversible. A quick search on google gives a number of posts that say as much.
Simple. But wrong! Just try this simple test.
- Insert records into a table.
- Open a transaction.
- Truncate the table.
- View the contents of the table.
- Rollback the transaction.
- View the contents of the table.
The code below is a very simple example:
Before the transaction is rolled back, there are no rows in the table, but after the rollback all three records have returned, despite having used Truncate to remove them. Clearly Truncate is reversible!CREATE TABLE MyTable (col1 CHAR(1), col2 INT)GOINSERT INTO MyTable (col1, col2)SELECT 'a', 1 UNION ALL SELECT 'b', 2 UNION ALL SELECT 'c', 3GOSELECT * FROM MyTable --(3 row(s) affected)BEGIN TRANTRUNCATE TABLE MyTableGOSELECT * FROM MyTable --(0 row(s) affected)ROLLBACKSELECT * FROM MyTable --(3 row(s) affected)
So what actually is the difference between Truncate and Delete? There are a number of general differences, but the relevant one here is the following:
When rows are DELETE'd the operation is logged for each row removed. TRUNCATE logs only the deallocation of the data pages rather than the rows themselves, making it a much faster operation.
There are many other differences related to permission levels, seeding, triggers, contraints etc. That's for a future post. For now, the point to note is that TRUNCATE can indeed be rolled back, and the myth has been busted!
This comment has been removed by a blog administrator.
ReplyDelete