请教一个sql触发器的问题

现在的需求是,向一个表tb_Student_Work插入一条记录前,将字段state为1的全部改为1
请问该怎么实现

我的触发器语句是

CREATE TRIGGER tri_tb_Student_WorkByUpdate
ON tb_Student_Work
FOR INSERT
AS
BEGIN
    DECLARE @subject char(50);
    --把新增的值赋给变量
    SELECT @subject=_subject FROM inserted;
    UPDATE tb_Student_Work SET _state = -1 WHERE _subject=@subject AND _state = 1
END

但结果是新插入的那条记录的state值也成了-1,我想让它的值不变

sql

1个回答

CREATE TRIGGER tri_tb_Student_WorkByUpdate
ON tb_Student_Work
FOR INSERT
AS
BEGIN
DECLARE @subject char(50);
--把新增的值赋给变量
SELECT @subject=_subject FROM inserted; --取主键
UPDATE tb_Student_Work SET _state = -1 WHERE _subject<>@subject AND _state = 1
END

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问