如何在 SQL Server 2005 中创建禁用的触发器?

发布于 2024-09-16 05:38:05 字数 75 浏览 3 评论 0原文

使用 Oracle 时,您可以创建禁用的触发器,并在触发器主体之前指定单词 DISABLE。如何在Sql Server中达到同样的效果?

When using Oracle you can create a disabled trigger specifing the word DISABLE before the trigger body. How can I achive the same effect in Sql Server?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

匿名。 2024-09-23 05:38:05

如果您确实必须创建禁用的触发器,则在事务中创建并禁用它:

begin tran
go
create trigger t_i on t after insert as begin /* trigger body */ end
go
disable trigger t_i on t
commit
go

GO 在那里,因为 CREATE TRIGGER 必须是批处理中的第一个语句,但根据您部署代码的方式,您可能可以使其看起来更整洁一些。

If you really must create the trigger disabled then create and disable it within a transaction:

begin tran
go
create trigger t_i on t after insert as begin /* trigger body */ end
go
disable trigger t_i on t
commit
go

The GOs are there because CREATE TRIGGER must be the first statement in a batch, but depending on how you deploy your code you can probably make it look a bit neater.

鹤舞 2024-09-23 05:38:05

我这样做的方法是执行创建和禁用,如下所示:

EXEC('CREATE TRIGGER trigger_on_myTable ON myTable <Trigger body> ');

EXEC('DISABLE TRIGGER trigger_on_myTable ON myTable');

这允许我在同一脚本中创建和禁用,而无需任何 GO。

The way I did it was to EXEC both the create and the disable like:

EXEC('CREATE TRIGGER trigger_on_myTable ON myTable <Trigger body> ');

EXEC('DISABLE TRIGGER trigger_on_myTable ON myTable');

This allowed me to create and disable in the same script without the any GO's.

百合的盛世恋 2024-09-23 05:38:05

如果您更喜欢不需要事务的解决方案,但也消除了触发器触发并在创建触发器和禁用触发器之间执行操作的机会,那么您可以基本上不使用任何代码来创建触发器在其中,然后禁用它,然后更改它以包含其实际主体:

create trigger dbo.MyTrigger
    on dbo.MyTable
    after insert

as

declare @Foo int;

--Trigger body must have at least a statement (or an "external name") so that's why the above dummy declare.

go

disable trigger dbo.MyTrigger
    on dbo.MyTable;

go

alter trigger dbo.MyTrigger
    on dbo.MyTable
    after insert

as

declare @Foo int;

--Remove above declare statement and insert actual trigger code here.

go

If you prefer a solution that doesn't require a transaction, but that also eliminates the chance of the trigger firing and doing its thing between the moment it is created and the moment it is disabled, then you can create the trigger with basically no code in it, then disable it, and then alter it to include its actual body:

create trigger dbo.MyTrigger
    on dbo.MyTable
    after insert

as

declare @Foo int;

--Trigger body must have at least a statement (or an "external name") so that's why the above dummy declare.

go

disable trigger dbo.MyTrigger
    on dbo.MyTable;

go

alter trigger dbo.MyTrigger
    on dbo.MyTable
    after insert

as

declare @Foo int;

--Remove above declare statement and insert actual trigger code here.

go
靑春怀旧 2024-09-23 05:38:05

第一次创建空触发器(不执行任何操作),
第二次禁用它,
第三步将其修改为所需的内容,
第四,需要时启用它

1st create empty trigger (performing no action),
2nd disable it,
3rd alter it with the desired content,
4th enable it when needed

小ぇ时光︴ 2024-09-23 05:38:05

T-SQL 提供了 DISABLE TRIGGER 语句来完成相同的事情。您可以在此处找到详细信息:禁用触发语法

T-SQL provides a DISABLE TRIGGER statement that accomplishes the same thing. You can find the details here: DISABLE TRIGGER SYNTAX

じ违心 2024-09-23 05:38:05

在管理工作室中,展开表下的触发器文件夹,然后右键单击触发器并禁用。

DISABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

MSDN:禁用触发器 (Transact-SQL)

ENABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

MSDN:启用触发器 (Transact-SQL)

sql-server-disable-all -数据库上的触发器禁用所有服务器上的所有触发器

In management studio Expand Trigger folder under table and Right Click on Trigger and Disable.

DISABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

MSDN:DISABLE TRIGGER (Transact-SQL)

ENABLE TRIGGER { [ schema_name . ] trigger_name [ ,...n ] | ALL }
ON { object_name | DATABASE | ALL SERVER } [ ; ]

MSDN:ENABLE TRIGGER (Transact-SQL)

sql-server-disable-all-triggers-on-a-database-disable-all-triggers-on-all-servers

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文