0

两张表:

    Customer: Cus_ID, Status
    Payment: Pay_ID, Cus_ID, PaidOrNot

要求是在将“Yes”设置为“PaidOrNot”时,然后自动将客户状态设置为“VIP”。

    CREATE TRIGGER AutoVIP

    AFTER UPDATE OR INSERT ON Payment

    BEGIN

        IF PaidOrNot = Yes
        THEN UPDATE Customer set Status = VIP

    END

我对SQL语法不太熟悉,这个触发器似乎不起作用,你能帮我修改一下吗?谢谢。

4

2 回答 2

1

对于 SQL Server,请尝试以下操作:

 CREATE TRIGGER AutoVIP
 ON Payment
 AFTER UPDATE, INSERT
 AS 
   BEGIN
     DECLARE @paid VARCHAR(5)
     DECLARE @custId INT
     SET @paid = (SELECT PaidOrNot FROM INSERTED)
     SET @custId = (SELECT Cus_ID FROM INSERTED)
     IF @paid = 'Yes'
     BEGIN
       UPDATE Customer set Status = 'VIP'
        where Cus_ID = @custId;
     END;
 END;
于 2012-11-22T23:51:18.107 回答
1

如果你尝试这个会发生什么?

我刻意试图让它尽可能不言自明。

CREATE TRIGGER AutoVIP
ON Payment
AFTER INSERT, UPDATE
AS
BEGIN
  DECLARE @PaidOrNot VARCHAR(3) -- Or whatever datatype it is really
  DECLARE @CustomerId INT -- Or whatever datatype it is
  SET @PaidOrNot = (SELECT PaidOrNot FROM INSERTED)
  SET @CustomerId = (SELECT Cus_ID FROM INSERTED)
  IF @PaidOrNot = 'Yes'
  BEGIN
    UPDATE Customer set Status = VIP WHERE Cus_ID = @CustomerId
  END
END
于 2012-11-23T00:10:48.653 回答