在 SQL Server 中创建触发器

发布于 2024-09-11 20:57:22 字数 168 浏览 4 评论 0原文

当我想使用 SQL Server 2008 R2 的预定义“CREATE TRIGGER”创建触发器时,我迷失了方向。您能否给我一个直接的 SQL 语句,我可以用它来创建触发器,并告诉我如何定义 AFTER、BEFORE 等?

另外,我如何知道行已更新/已插入/已删除,并使用它们的列值在触发器内执行操作?

I got lost when I wanted to create trigger using the pre-defined "CREATE TRIGGER" of SQL Server 2008 R2. Could you please give me a direct SQL statement that I can use to create a trigger, and tell me how to define AFTER, BEFORE, and all that?

Also, how can I know the rows UPDATED/INSERTED/DELETED, and use their column values to do operations inside the trigger?

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

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

发布评论

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

评论(3

童话 2024-09-18 20:57:22

基本语法是

CREATE TRIGGER YourTriggerName ON dbo.YourTable
FOR|AFTER INSERT, UPDATE, DELETE
AS
BEGIN
     /*Put what ever you want here*/
     UPDATE AnotherTable
          SET SomeColumn = AnotherColumn
     FROM inserted | deleted
END
GO

The basic syntax is

CREATE TRIGGER YourTriggerName ON dbo.YourTable
FOR|AFTER INSERT, UPDATE, DELETE
AS
BEGIN
     /*Put what ever you want here*/
     UPDATE AnotherTable
          SET SomeColumn = AnotherColumn
     FROM inserted | deleted
END
GO
药祭#氼 2024-09-18 20:57:22

数据库是面向集合的,触发器也不例外。当执行给定操作并且该操作可能影响多行时,触发器将触发。因此,问题“假设我想知道该行的主键”是用词不当。可以插入多行。

SQL Server 为 AFTER 触发器提供了两个名为 inserteddeleted 的特殊表,它们表示由操作插入或删除的行,其结构与受影响的表相同。更新触发器可能会填充inserteddeleted,而插入触发器只会填充inserted 表。

来自评论:

但是电子邮件收件人将根据第二个表中的值来决定,其中外键 ID 位于第一个表中(即带有触发器的表)

这个问题的答案是使用 inserted 表(同样,您必须假设可以有多行)来循环显示行并发送电子邮件。但是,我建议不要将电子邮件逻辑放入触发器中,而是建议将该逻辑放入存储过程中,然后。从中发送您的电子邮件

以供参考:创建触发器

Databases are set-oriented and triggers are no different. A trigger will fire when a given operation is performed and that operation might affect multiple rows. Thus, the question "Say I want to know the Primary Key of that row" is a misnomer. There could be multiple rows inserted.

SQL Server provides two special tables for AFTER triggers named inserted and deleted which represent the rows that were inserted or deleted by an action and are structured identically to the table being affected. An update trigger might populate both inserted and deleted whereas an insert trigger would only populate the inserted table.

From comments:

but the email recipient will be decided based on a value in a second table, where the foreign key ID is located in the first table (which is the one with trigger

The answer to this question is to use the inserted table (which again, you must assume could have multiple rows) to cycle through the rows and send an email. However, I would recommend against putting email logic in a trigger. Instead, I would recommend putting that logic in a stored procedure and send your email from that.

For reference: Create Trigger

萌辣 2024-09-18 20:57:22

触发器是一个基于事件的过程,在表以某种方式更改后“触发”。这将发生在删除、更新、插入等操作上。 BEFORE 和 AFTER 语法将定义是在提交事件之前还是之后运行触发器。

这是简短的版本。请查看 MSDN

A trigger is an event-based process that is "triggered" after a table is changed in some way. This will be on DELETE, UPDATE, INSERT, and so forth. Your BEFORE and AFTER syntax will define whether to run the trigger before or after the event is committed.

That's the short version. Check out MSDN.

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