简体   繁体   中英

what to return in postgres function

I have function to reduce stock_qty of product by the qty in the order

CREATE or REPLACE function update_stock_quantity()
returns TRIGGER AS
$$
BEGIN

UPDATE products set quantity_stock = products.quantity_stock - order_details.quantity_stock 
FROM products INNER JOIN order_details on products.id = order_details.product_id;
END; 
$$
language plpgsql

with a trigger function on order_detail table

CREATE TRIGGER set_stock_qty AFTER
INSERT
    ON order_details FOR EACH ROW EXECUTE PROCEDURE update_stock_quantity();

and I get this error error: control reached end of trigger procedure without RETURN

how to use return in the function

You must use RETURN, adding the code line "RETURN NEW;":

CREATE or REPLACE function update_stock_quantity()
returns TRIGGER AS
$$
BEGIN

UPDATE products set quantity_stock = products.quantity_stock - order_details.quantity_stock 
FROM products INNER JOIN order_details on products.id = order_details.product_id;
RETURN NEW;
END; 
$$
language plpgsql

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM