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

我如何引用刚刚在课程注册中插入的内容?

最佳答案

试试这个:

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 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 ...

10-06 05:41