根据表b中的条件更新表中的列

时间:2016-02-11 17:04:06

标签: sql-server triggers

我正在尝试在Table1中编写一个触发器,根据Table2中的条件更新Table1中的列。我在SQL方面不是那么好但是足够危险。此脚本无法识别col2中的列Table1

Create TRIGGER trgupd on Table1 
After insert
AS 
    If Inserted.col2 = '1'
    BEGIN
        Update Table2
        Set col3 = '2000' + the inserted value
        From Table2
        inner join inserted I on Table1.ID = Table2.ID
    End

1 个答案:

答案 0 :(得分:0)

只需将其更新为:

Create TRIGGER trgupd on Table1 
After insert
AS 
    BEGIN
    Update Table2
    Set col3 = '2000' + the inserted value
    From Table2
    inner join inserted I on I.ID = Table2.ID -- note I.ID not Table1.ID
    where I.col2 = 1;
END

你的失败的原因是insert是一个表或记录集,inserted.col2实际上没有选择一行。只需在更新中添加where子句即可。

另外,请注意插入的连接:Table1错误(它不在该查询中);它需要是我。