Can we possible to call truncate command inside trigger using sql server?
-
2Surely as quick to try it as post a questionMartin Smith– Martin Smith2022-01-31 08:16:46 +00:00Commented Jan 31, 2022 at 8:16
-
1Yes, you can. Better question is why would you want to...Charlieface– Charlieface2022-01-31 09:42:18 +00:00Commented Jan 31, 2022 at 9:42
-
@Charlieface You can delete faster with truncate. As an example to delete history in a tracking table, everytime one row is added to a dba table to oversee who is working... Do not forget that TRUNCATE is an operation that does'nt fire the trigger DELETE... Whch can be interesting for certain use cases.SQLpro– SQLpro2022-01-31 10:07:48 +00:00Commented Jan 31, 2022 at 10:07
Add a comment
|
1 Answer
TRUNCATE command is a transactional command and all the pages deleted will be writed into the transaction log. And because the trigger code is envolved in a transaction, you can ROLLBACK or COMMIT the implicit transaction into the trigger code (COMMIT is not a good practice into trigger code).
So, yes, you can !