declare @sql nvarchar(150)
declare trigger_cursor CURSOR FOR
select 'DISABLE TRIGGER ' + SCHEMA_NAME(schema_id)+'.'+name
+' ON '+SCHEMA_NAME(schema_id)+'.'+ OBJECT_NAME(parent_object_id)
from sys.objects
where type = 'TR'
OPEN trigger_cursor
FETCH NEXT FROM trigger_cursor INTO @sql
WHILE @@FETCH_STATUS = 0
BEGIN
print @sql
--sp_executesql @sql
FETCH NEXT FROM trigger_cursor INTO @sql
END
CLOSE trigger_cursor
DEALLOCATE trigger_cursor
-- this script only returns the triggers
-- in an specific database or server triggers
use your_database
declare @TABLEIDOUT int=NULL -- this is the object_id of a table
-- if not null then only the triggers for that
-- table are considered
declare @include_server_triggers bit = 1
-- if 1 then the server triggers are
-- considered otherwise they are ignored
SELECT _enable_ =N'use ' + QUOTENAME(DB_NAME()) + N'; ENABLE TRIGGER '
+ st.name + N' ON ' + st.parent_name
,_disable_ =N'use ' + QUOTENAME(DB_NAME()) + N'; DISABLE TRIGGER '
+ st.name + N' ON ' + st.parent_name
,_name_ = st.name
,ST.is_disabled
FROM (
SELECT
object_id=s.object_id
,parent_name = QUOTENAME(OBJECT_SCHEMA_NAME(s.parent_id)) + '.'
+ QUOTENAME(OBJECT_NAME(s.parent_id))
,name = QUOTENAME(OBJECT_SCHEMA_NAME(s.object_id)) + '.'
+ QUOTENAME(OBJECT_NAME(s.object_id))
,s.is_disabled
FROM sys.triggers s
WHERE 1=1
AND ((s.parent_id = @TABLEIDOUT) OR (@TABLEIDOUT is null))
) st
WHERE ST.name IS NOT NULL
UNION ALL
SELECT _enable_ =N'use master; ENABLE TRIGGER ' + QUOTENAME(st.name)
+ N' ON ALL SERVER'
,_disable_ =N'use master; DISABLE TRIGGER ' + QUOTENAME(st.name)
+ N'ON ALL SERVER'
,_name_ = st.name
,ST.is_disabled
FROM master.sys.server_triggers st
WHERE @include_server_triggers =1
对于 DML 触发器,您可以使用光标下方。如果您还需要禁用
DDL
触发器,则需要根据sys.triggers
视图创建类似的光标。(因为sys.objects
我们只有 DML 游标)在禁用触发器之前,请考虑一种保持数据完整性的方法,例如,如果有 10 个插入出现并且其中一些触发触发器,那么您禁用它们触发器,然后下一个插入不会触发触发器 - 那么我们有现在有问题。如果您不考虑这一点,那么后果很可能对您和/或您的公司不利。
例如,如果您的环境和工作负载以及系统的繁忙程度,您可以将数据库设置为单用户模式。
至少考虑一下对您的环境的影响。
您还可以编写触发器脚本。
启用/禁用触发器的脚本 - 脚本生成
您可以检查触发器何时被禁用。它不记录在默认跟踪中。