Search this blog

Wednesday, December 29, 2010

Truncate Within A Transaction

In the previous post, we have seen, what is the difference between in “Truncate vs. Delete”?


Actually if we use truncate then we can’t rollback the data, but the question is … What will happen if we use truncate within a transaction, Whether we can rollback or not?.


The following example reveals the answer for our question.


I created a table and inserted a rows, as shown below


Then I executed the below Query:



It Returns:

Conclusion:
Truncated Data can be roll backed if it is executed within a transaction

Truncate Vs Delete

Truncate

Delete

Truncate Table <Table Name>

Delete from <Table Name> Where <Condition>

Cannot use Where Condition

It is possible

It Removes all the data

It deletes specified data if where condition exists.

Faster in performance wise, because it doesn't keep any logs

Slower than truncate because, it keeps logs

Rollback is not possible

Rollback is possible