我正在尝试创建一个触发器,该触发器会设置一个功能,以便在将图书的货件插入表格货件时检查是否有足够的库存。如果库存不足,则会引发异常,否则库存减少1。
这是我的代码:
CREATE FUNCTION decstock() RETURNS trigger AS $pname$
BEGIN
IF (SELECT stock FROM stock WHERE stock.isbn = NEW.ISBN) = 0 THEN
RAISE EXCEPTION 'There is no stock to ship'
ELSE
UPDATE stock
SET stock = stock-1
WHERE stock.isbn = NEW.isbn;
END IF;
END;
$pname$ LANGUAGE plpgsql;
CREATE TRIGGER hej
BEFORE UPDATE ON shipments
FOR EACH ROW /*Why on each row? Aren't we supposed to only look at the stock for the specified isbn? Also, how do I reference the stock table here?*/
EXECUTE PROCEDURE decstock();
运行时遇到错误,它说:
ERROR: syntax error at or near "ELSE"
LINE 7: ELSE
^
********** Error **********
ERROR: syntax error at or near "ELSE"
SQL state: 42601
Character: 181
我找不到我在这里做的语法错误。有什么提示吗?
答案 0 :(得分:2)
RAISE EXCEPTION '....'
必须以分号结束,就像任何其他pl / pgsql语句一样。