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
如何参考在课程中插入的内容?
答案 0 :(得分:4)
试试这个:
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 ...
答案 1 :(得分:1)
您可以从插入的表中获取值
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
答案 2 :(得分:0)
有两个有用的触发器元表,它们都与触发器所在表的模式匹配,INSERTED和DELETED。
有趣的是,如果运行更新语句,触发器元表将包含INSERTED和DELETED中的行,DELETED行对应于“更新前”,INSERTED对应“更新后”。
这也使其有助于审计目的。