Wednesday, February 25, 2009

T-SQL - TRUNCATE and IDENTITY

If you want to delete all records in a table, the fastest way is through TRUNCATE. It will deallocate data pages used to store the table’s data, and only the page deallocations are recorded in the transaction log. but "DELETE FROM tablename" removes records one by one.

Syntax: TRUNCATE TABLE tablename
  • When the table referenced by a FOREIGN KEY constraint, TRUNCATE TABLE is not applicable; instead, use DELETE statement without a WHERE clause.
  • TRUNCATE TABLE doesn't do log, then it cannot activate a trigger.
  • TRUNCATE TABLE will reset IDENTITY value to its base value then if you want to have another value for your IDENTITY enabled column, you need to call DBCC CHECIDENT

Share/Bookmark

No comments: