如何在更新或插入后将正插入值转换为负值?

时间:2015-12-23 13:36:34

标签: sql-server triggers sql-update sql-insert

我想编写一个触发器,该触发器应该查找插入或更新的值是否为正,并将其替换为负值。 我试过这段代码:

create trigger trigger_after_updaten on Inbox
AFTER Update 
AS
begin
DECLARE @t INT 
If ((SELECT @t=ID_In FROM updated)>0) 
UPDATE Inbox SET t=@t*-1
END

但是它显示了这个错误:

Msg 102, Level 15, State 1, Procedure trigger_after_updaten, Line 223
Incorrect syntax near '='.

如何以正确的方式做到这一点?

1 个答案:

答案 0 :(得分:0)

这适合您的触发器:

CREATE TRIGGER trigger_after_updaten on Inbox
AFTER UPDATE 
AS
BEGIN
    UPDATE Inbox SET t = CASE WHEN(inserted.t < 0) THEN inserted.t ELSE -inserted.t END
    FROM inserted
    INNER JOIN Inbox 
        ON inserted.KEY_FIELD = Inbox.KEY_FIELD
END