Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL trigger on Truncate

How to Fire a trigger when you do TRUNCATE (instead deleted) in MSSQL

like image 615
Sreedhar Avatar asked Feb 10 '10 03:02

Sreedhar


People also ask

Can we apply trigger on TRUNCATE?

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.

Does TRUNCATE fire triggers?

TRUNCATE TABLE cannot be used when a foreign key references the table to be truncated, since TRUNCATE TABLE statements do not fire triggers. This could result in inconsistent data because ON DELETE / ON UPDATE triggers would not fire.

Does TRUNCATE delete triggers?

To achieve high performance, TRUNCATE TABLE bypasses the DML method of deleting data. Thus, it does not cause ON DELETE triggers to fire, it cannot be performed for InnoDB tables with parent-child foreign key relationships, and it cannot be rolled back like a DML operation.

Does the database trigger fire when the table is truncated?

No, TRUNCATE is a DDL statement and does not fire any trigger on that table.


2 Answers

From msdn:

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions.

like image 88
Sam Avatar answered Oct 10 '22 12:10

Sam


You can't do this on SQL server.

From MSDN

TRUNCATE TABLE cannot activate a trigger because the operation does not log individual row deletions. For more information, see CREATE TRIGGER (Transact-SQL).

like image 35
David Hall Avatar answered Oct 10 '22 14:10

David Hall