在此触发器的Orderlist中插入行时收到错误:
ERROR: column "qty_in_stock" does not exist
LINE 1: SELECT qty_in_stock > 0
^
QUERY: SELECT qty_in_stock > 0
CONTEXT: PL/pgSQL function books_upd() line 6 at CASE
****** Error **********
ERROR: column "qty_in_stock" does not exist
SQL state: 42703
Context: PL/pgSQL function books_upd() line 6 at CASE
这是我的代码:
INSERT INTO orderlist VALUES (12,1235,6)
CREATE OR REPLACE FUNCTION books_upd()
RETURNS trigger as $bookupd$
BEGIN
UPDATE books
SET qty_in_stock=qty_in_stock - NEW.quantity
WHERE isbn=NEW.isbn;
CASE WHEN qty_in_stock > 0 THEN
UPDATE stockmanager
set quantity=quantity+NEW.quantity
WHERE isbn=NEW.isbn;
END CASE;
RETURN NEW;
END;
$bookupd$ LANGUAGE plpgsql;
许多关于在qty_in_stock周围放置引号的论坛建议 - 同样的错误。
表格信息:
表格书籍"" isbn"" -pk ""标题"" ""作者"" "" qty_in_stock"" ""价格"" ""成本"" ""年出版的"" ""的publisherId""
表stockmanager: "" ISBN"" -pk,FK ""量""
任何帮助表示赞赏。
答案 0 :(得分:1)
您不能使用这样的CASE语句,也不能在SQL语句之外引用表的列。您需要做的是将新值存储在变量中,然后将其与if
语句一起使用:
CREATE OR REPLACE FUNCTION books_upd()
RETURNS trigger as
$bookupd$
DECLARE
l_qty integer;
BEGIN
UPDATE books
SET qty_in_stock = qty_in_stock - NEW.quantity
WHERE isbn=NEW.isbn
RETURNING qty_in_stock INTO l_qty;
if l_qty > 0 THEN
UPDATE stockmanager
set quantity= quantity + NEW.quantity
WHERE isbn = NEW.isbn;
END IF;
RETURN NEW;
END;
$bookupd$
LANGUAGE plpgsql;