Pages

Sunday, January 1, 2017

Enable or Disable triggers of SQL Server tables

Script to Enable or Disable triggers of SQL Server tables


DBA need a script to enable or disable triggers on one or multiple tables of SQL Server depending upon the demand from application or development teams.

Usually we disable triggers when we archive data or do a bulk insert into a particular table.

Please find the below code to generate enable or disable trigger script. In the below code, 
three steps are commented.
To enable triggers, uncomment line 2.
To disable triggers, uncomment line 3
To put a condition for a particular table, uncomment line 8.



Declare @input varchar(10)

--set @input ='ENABLE'

--set @input='DISABLE'

select +@input+' TRIGGER [' +DB_NAME()+'].['+SCHEMA_NAME(a.schema_id)+
'].['+b.name+'] ON [' +DB_NAME()+'].['+SCHEMA_NAME(a.schema_id)+'].['+a.name+']'
from sys.objects a inner join sys.triggers b 
on a.object_id=b.parent_id
--where a.name like 'objectname/tablename'
order by a.name