我有以下触发器:
CREATE TRIGGER trigger_insert_student_table ON student_table
FOR INSERT
AS
BEGIN
SET NOCOUNT ON;
DECLARE @insertedStudentName VARCHAR(256);
DECLARE @insertedMajor VARCHAR(256);
DECLARE @insertedGrade VARCHAR(256);
SET @insertedStudentName = (SELECT STUDENT_NAME FROM inserted);
SET @insertedMajor = (SELECT MAJOR FROM inserted);
SET @insertedGrade = (SELECT GRADE FROM inserted);
IF (@insertedMajor='Economics' AND @insertedGrade = 'A')
BEGIN
/* DO SOME ACTION*/
END
ELSE IF(@insertedMajor='History' AND @insertedGrade = 'A')
BEGIN
/* DO SOME ACTION*/
END
END
它适用于单个插入,但是当我执行多行插入时,它会失败并出现以下错误:
子查询返回的值超过1。这是不允许的 子查询跟随=,!=,<,< =,>,> =或当子查询用作 表达。
如何遍历每个插入的行并处理操作?
答案 0 :(得分:2)
我不知道"某些动作"是你想在这里做的,但这只是一个简单的例子:
update t
set UpdateDate = getdate(),
isStarEconomicsStudent = 1
from somePresentationTable a
inner join inserted i
on a.StudentID = i.StudentID
and i.Major = 'Economics'
and i.Grade = 'A'