Skip to content

Commit 0f8406d

Browse files
committed
Add Disable_Enable_All_Triggers_In_Database.sql scipt
1 parent f63c15f commit 0f8406d

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
<documentation>
3+
<summary>Disable and enable again all triggers in database</summary>
4+
<returns>nothing.</returns>
5+
<issues>No</issues>
6+
<author>Federico Minca</author>
7+
<created>2019-08-20</created>
8+
<modified>2019-08-26 by Konstantin Taranov</modified>
9+
<version>1.0</version>
10+
<sourceLink>https://github.com/ktaranov/sqlserver-kit/blob/master/Scripts/Disable_Enable_All_Triggers_In_Database.sql</sourceLink>
11+
<originalLink>https://jesspomfret.com/disable-all-triggers/</originalLink>
12+
</documentation>
13+
*/
14+
15+
-- Select Active Triggers and prepare Disable and Enable T-SQL Statement and put into Temp Table
16+
SELECT 'ALTER TABLE [' + sc.name + '].[' + ta.name + '] DISABLE TRIGGER [' + tr.name + '];' AS DisableTriggerStatement,
17+
'ALTER TABLE [' + sc.name + '].[' + ta.name + '] ENABLE TRIGGER [' + tr.name + '];' AS EnableTriggerStatement
18+
INTO #Triggers
19+
FROM sys.triggers tr
20+
INNER JOIN sys.tables ta ON ta.object_id = tr.parent_id
21+
INNER JOIN sys.schemas sc ON sc.schema_id = ta.schema_id
22+
WHERE tr.is_disabled = 0;
23+
24+
-- Perpare Variable Script for Execution
25+
DECLARE @Disable_Statement nvarchar(max) = (
26+
SELECT tmp.DisableTriggerStatement + CHAR(10) AS "data()"
27+
FROM #Triggers tmp
28+
FOR XML PATH('')
29+
);
30+
31+
-- Execute SQL
32+
EXEC sys.sp_executesql @Disable_Statement;
33+
34+
-- Perpare Variable Script for Execution
35+
DECLARE @Enable_Statement nvarchar(max) = (
36+
SELECT tmp.DisableTriggerStatement + CHAR(10) AS "data()"
37+
FROM #Triggers tmp
38+
FOR XML PATH('')
39+
);
40+
41+
-- Execute SQL
42+
EXEC sys.sp_executesql @Enable_Statement;
43+
44+
DROP TABLE #Triggers;

0 commit comments

Comments
 (0)