如何引用刚刚插入的值(在 TRIGGER 语句中)?

发布于 2024-11-07 23:42:08 字数 268 浏览 0 评论 0原文

CREATE TRIGGER ATTENDANCE_INSERTION_TRIGGER
ON course_enrollment
AFTER INSERT
AS
BEGIN

--insert **what just was inserted in course_enrollment** into course_schedule_attendance

END
GO

如何引用刚刚在 course_enrollment 中插入的内容

CREATE TRIGGER ATTENDANCE_INSERTION_TRIGGER
ON course_enrollment
AFTER INSERT
AS
BEGIN

--insert **what just was inserted in course_enrollment** into course_schedule_attendance

END
GO

How do I refer to what just was inserted in course_enrollment ?

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

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

发布评论

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

评论(3

神回复 2024-11-14 23:42:08

试试这个:

CREATE TRIGGER ATTENDANCE_INSERTION_TRIGGER 
ON course_enrollment 
AFTER INSERT 
AS 
BEGIN

  --insert **what just was inserted in course_enrollment** into
  -- course_schedule_attendance  
  INSERT course_schedule_attendance  
      (course_id, student_id)
    SELECT
      course_id, student_id --you could use: INSERTED.course, INSERTED.student_id
      FROM INSERTED
END 
GO 

您也可以在单个 INSERT 中执行此操作,无需触发器:

--insert a single row in both tables at one time
INSERT course_enrollment 
      (course_id, student_id)
      OUTPUT course_id, student_id INTO course_schedule_attendance  
   VALUES (@xyz, @abc)

--or insert a set of rows into both at one time
INSERT course_enrollment 
      (course_id, student_id)
      OUTPUT course_id, student_id INTO course_schedule_attendance  
   SELECT
       xyz, abc
       FROM ...

try this:

CREATE TRIGGER ATTENDANCE_INSERTION_TRIGGER 
ON course_enrollment 
AFTER INSERT 
AS 
BEGIN

  --insert **what just was inserted in course_enrollment** into
  -- course_schedule_attendance  
  INSERT course_schedule_attendance  
      (course_id, student_id)
    SELECT
      course_id, student_id --you could use: INSERTED.course, INSERTED.student_id
      FROM INSERTED
END 
GO 

you could also do this in a single INSERT, without the trigger:

--insert a single row in both tables at one time
INSERT course_enrollment 
      (course_id, student_id)
      OUTPUT course_id, student_id INTO course_schedule_attendance  
   VALUES (@xyz, @abc)

--or insert a set of rows into both at one time
INSERT course_enrollment 
      (course_id, student_id)
      OUTPUT course_id, student_id INTO course_schedule_attendance  
   SELECT
       xyz, abc
       FROM ...
超可爱的懒熊 2024-11-14 23:42:08

您可以从插入的表中获取值

SELECT * FROM INSERTED I

因此,例如,如果 course_enrollment 有一个名为 course_id 和 Student_id 的字段,要获取这些字段,您可以执行以下操作

SELECT I.course_id,I.student_id FROM INSERTED I -- I is just an alias and is not needed but it helps

You can get the values from the inserted table

SELECT * FROM INSERTED I

So for example if course_enrollment had a field called course_id and student_id, to get those fields you can do a

SELECT I.course_id,I.student_id FROM INSERTED I -- I is just an alias and is not needed but it helps
淑女气质 2024-11-14 23:42:08

有两个有用的触发器元表,它们都与触发器所在的表的架构相匹配:INSERTED 和 DELETED。

有趣的是,如果您运行更新语句,触发器元表将在 INSERTED 和 DELETED 中都有行,DELETED 行对应于“更新之前”,而 INSERTED 对应于“更新后”。

这也使其对于审计目的很有用。

There are two useful trigger meta-tables, both of which match the schema of the table that the trigger is on, INSERTED and DELETED.

Interestingly if you run an update statement, the trigger meta tables will be have rows in both INSERTED and DELETED, the DELETED rows corresponding to "before the update" and INSERTED with "after the update".

This also makes it useful for audit purposes.

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