If you want to remove data from a table, you can do this in a couple of ways using TRUNCATE TABLE or DELETE. A common approach is to use DELETE to remove all rows in the table but TRUNCATE TABLE offers an alternative and I will list what the differences between the two are.
--Delete rows with filter DELETE FROM SchemaName.Table WHERE...
--Delete all rows DELETE SchemaName.Table
--Delete all rows DELETE FROM SchemaName.Table
--Delete using JOINS DELETE T1 FROM SchemaName.Table1 AS T1 JOIN SchemaName.Table2 AS T2 ON T1.Column1 = T2.Column1
--Delete using IN DELETE FROM SchemaName.Table1 WHERE Column1 IN (SELECT Column1 FROM SchemaName.Table2)
--Delete using EXISTS DELETE T1 FROM SchemaName.Table1 AS T1 WHERE EXISTS (SELECT * FROM SchemaName.Table2 AS T2 WHERE T1.Column1 = T2.Column1)
- Provides more control over what data is and can be deleted.
- Will not reseed the identity value on an identity column
- Can be used where the DELETE operation does not violate foreign key constraint
- Can be a slow operation if the table is large
- Locking can be an issue if the operation is removing many rows
- Will fire triggers
- Transactions can be rolled back as LSN (log sequence number) is maintained in the transaction log
- Data pages consumed may need to be reclaimed using a DBCC SHRINKDATABASE operation
- DELETE will typically consume more transaction log space because of extra logging
TRUNCATE TABLE Schema.TableName;
- WHERE clause is not optional – it will remove all data from your table
- Reseeds the identify value of an identity column
- Cannot be used on a table which is referenced by a foreign key constraint
- Can be considerably quicker than DELETE due to deallocation of data pages and minimal logging
- Cannot be used in replication or log shipping
- Will not fire triggers
- Cannot be used on tables which are part of an indexed view
- Who has permission: table owner, sysadmin, db_owner, db_ddladmin or any user with ALTER permission on the table
I wanted to expand on the differences in speed between the two operations.
When TRUNCATE TABLE is issued against a table, all the data pages are deallocated which are used to store the tables data. Compared with a DELETE, this is significantly faster as each DELETE operation is logged in the transaction log but only the deallocations are logged in the transaction log for a TRUNCATE operation.
The DELETE operation will consume database resource and locks and can therefore be considerably slower and have greater impact on your system in comparison to TRUNCATE TABLE. See this post on reducing overhead for large delete operations.
As each DELETE operation is logged inside of the transaction log, it is possible to rollback to a point in time but with TRUNCATE TABLE it is not possible to have this level of precision. In a rollback scenario, only a complete rollback could be performed.
As with a DELETE it is also possible to rollback a TRUNCATE if it has not yet been committed. You can do this by using transactions like in this very simple example:
BEGIN TRAN TRUNCATE TABLE SchemaName.Table --test success here ROLLBACK TRAN ELSE COMMIT TRAN
Time to wrap this up..
So there are a number of differences between the two. Both operations have their pros and cons and provide you with a choice over how you want to remove your data.