为什么这个SQL更新语句不起作用?

时间:2015-11-27 09:34:52

标签: sql-server tsql triggers

我无法做到这一点......

如果同一行的gift_id,我需要将reservation_id IS NOT NULL值设置为NULL。

我想要实现的是,如果我更新该行的reservation_id列,则删除某行的gift_id列。

CREATE TRIGGER add_reservation 
AFTER UPDATE ON [dbo].[order] 
FOR EACH ROW 
BEGIN
    IF [reservation_id] IS NOT NULL 
        THEN SET [gift_id] = NULL 
    END IF 
END

错误:

  

Msg 102,Level 15,State 1,Procedure add_reservation,Line 2
  'AFTER'附近的语法不正确。

     

Msg 156,Level 15,State 1,Procedure add_reservation,Line 6
  关键字'THEN'

附近的语法不正确

2 个答案:

答案 0 :(得分:0)

我写了一个替代方案,以防你没有得到原始代码,这段代码需要你假设存在的表中的字段OrderID

  std::vector<std::vector<Told>> source;
  std::vector<std::vector<Tnew>> dest(source.size());
  std::transform(source.begin(), source.end(), dest.begin(), 
    [](std::vector<Told> const &v){ return std::vector<Tnew>(v.begin(), v.end()); });

答案 1 :(得分:0)

尝试这个

CREATE TRIGGER [DBO].[TRG_ORDER_UPDATE]
ON [TABLE_NAME]
AFTER UPDATE
AS
  BEGIN
      IF EXISTS(SELECT *
                FROM   INSERTED)
         AND EXISTS(SELECT *
                    FROM   DELETED)
; WITH TEST AS (SELECT <YOUR,COLUMNS> FROM INSERTED)
UPDATE TN SET TN.GIFT_ID = NULL 
FROM TABLE_NAME TN JOIN TEST T ON TN.(PRIMARY_COLUMN)=T.(PRIMARY_COLUMN)
WHERE T.RESERVATION_ID IS NOT NULL
END