1

请解决我的问题

create trigger DeleteProduct
Before delete on Product
BEGIN
    select CASE WHEN ((SELECT Inventory.InventoryID FROM Inventory WHERE Inventory.ProductID = OLD.ProductID and Inventory.Quantity=0) ISNULL)
    THEN    
           RAISE(ABORT,'Error code 82')
    Else   
          DELETE from inventory where inventory.ProductID=OLD.ProductID;
    END;

END;

删除语句附近的错误

4

2 回答 2

2

您不能将语句 likeDELETE放入表达式 likeCASE中。

在一般情况下,您可以使用WHEN 子句使触发器有条件:

CREATE TRIGGER DeleteProductError
BEFORE DELETE ON Product
WHEN NOT EXISTS (SELECT InventoryID
                 FROM Inventory
                 WHERE ProductID = OLD.ProductID
                   AND Quantity = 0)
BEGIN
    SELECT RAISE(ABORT, 'Error code 82');
END;

CREATE TRIGGER DeleteProduct
BEFORE DELETE ON Product
WHEN EXISTS (SELECT InventoryID
             FROM Inventory
             WHERE ProductID = OLD.ProductID
               AND Quantity = 0)
BEGIN
    DELETE FROM Inventory
    WHERE ProductID = OLD.ProductID;
END;
于 2013-10-08T13:37:02.213 回答
1

尝试这个:

CREATE TRIGGER DeleteProduct
BEFORE DELETE ON Product
BEGIN
    SELECT CASE WHEN (SELECT Inventory.InventoryID FROM Inventory WHERE Inventory.ProductID = OLD.ProductID and Inventory.Quantity=0) IS NULL
    THEN RAISE(ABORT,'Error code 82')
    END;
    -- If RAISE was called, next isntructions are not executed.
    DELETE from inventory where inventory.ProductID=OLD.ProductID;
END;
于 2013-10-08T13:33:52.687 回答