禁用表的启用触发器 SQL 服务器

我想创建一个如下的过程,但它有语法错误。 有人能指出这个问题吗?

Create PROCEDURE [dbo].[my_proc] AS


BEGIN


DISABLE TRIGGER dbo.tr_name ON dbo.table_name


-- some update statement


ENABLE TRIGGER dbo.tr_name  ON dbo.table_name


END


** Error Message : Incorrect syntax near 'ENABLE'.
219497 次浏览

使用以下命令:

ALTER TABLE table_name DISABLE TRIGGER tr_name


ALTER TABLE table_name ENABLE TRIGGER tr_name

之前的行需要以 ;结束,因为在 SQL DISABLE 不是关键词中:

BEGIN
;
DISABLE TRIGGER ...

正如 Mark 提到的,前面的语句应该以分号结尾,所以你可以使用:

; DISABLE TRIGGER dbo.tr_name ON dbo.table_name

下面是最简单的方法

试试密码

ALTER TRIGGER trigger_name DISABLE

就是这样:)

在新行中的 ENABLE TRIGGER 或 DISABLE TRIGGER 之后,写下 GO,示例:

DISABLE TRIGGER dbo.tr_name ON dbo.table_name


GO
-- some update statement


ENABLE TRIGGER dbo.tr_name  ON dbo.table_name


GO

下面是用于启用或禁用触发器的动态脚本。

select 'alter table '+ (select Schema_name(schema_id) from sys.objects o
where o.object_id = parent_id) + '.'+object_name(parent_id) + ' ENABLE TRIGGER '+
Name as EnableScript,*
from sys.triggers t
where is_disabled = 1

如果要直接从源执行 ENABLE TRIGGER:

我们不能这样写:

Conn.Execute "ENABLE TRIGGER trigger_name ON table_name"

相反,我们可以写:

Conn.Execute "ALTER TABLE table_name DISABLE TRIGGER trigger_name"

我想和大家分享一些帮助我走出困境的东西。

我需要一些能够禁用和重新启用特定表触发器的脚本。我通常尽量远离跳跳虎,但有时它们可以很好地利用。

我使用了上面的脚本,并向 sysoobject 添加了一个连接,这样我就可以根据表名进行筛选。此脚本将禁用表的一个或多个触发器。

select 'alter table '+ (select Schema_name(schema_id) from sys.objects o
where o.object_id = parent_id) + '.'+object_name(parent_id) + ' ENABLE TRIGGER '+ t.Name as EnableScript,*
from sys.triggers t
INNER JOIN dbo.sysobjects DS ON DS.id = t.parent_id
where is_disabled = 0 AND DS.name = 'tblSubContact'
USE [DatabaseName]
GO


-- HABILITAR TRIGGERS
SELECT 'ALTER TABLE ['+
( SELECT SCHEMA_NAME(SCHEMA_ID) FROM [sys].[objects] AS O WHERE O.[object_id] = T.[parent_id])
+ '].[' + OBJECT_NAME(T.[parent_id]) + '] ENABLE TRIGGER '+ T.[name] + ';' AS [EnableScript], *
FROM [sys].[triggers] AS T
INNER JOIN [sys].[sysobjects] DS ON DS.[id] = T.[parent_id]
WHERE T.[is_disabled] = 0
--AND DS.[name] = 'TableName'


-- DESHABILITAR TRIGGERS
SELECT 'ALTER TABLE ['+
( SELECT SCHEMA_NAME(SCHEMA_ID) FROM [sys].[objects] AS O WHERE O.[object_id] = T.[parent_id])
+ '].[' + OBJECT_NAME(T.[parent_id]) + '] DISABLE TRIGGER '+ T.[name] + ';' AS [EnableScript], *
FROM [sys].[triggers] AS T
INNER JOIN [sys].[sysobjects] DS ON DS.[id] = T.[parent_id]
WHERE T.[is_disabled] = 0
--AND DS.[name] = 'TableName'

习惯性触发器 选择“ ALTER TABLE [’+” (SELECT SCHEMA _ NAME (SCHEMA _ ID) FROM [ sys ] . [ Objects ] AS O.WHERE O. [ object _ id ] = T. [ father _ id ]) +’] . [’+ OBJECT _ NAME (T. [ father _ id ]) +’] ENABLE TRIGGER’+ T. [ name ] +’;’AS [ EnableScript ] ,* FROM [ sys ]。[触发器] AS T 内连接[ sys ] . [ sysoObjects ] DS ON DS. [ id ] = T [ father _ id ] 其中 T [ is _ disable ] = 1——必须是1,而不是0—— —— AND DS. [ name ] = ‘ TableName’