I wrote a simple trigger in PL/pgSQL for PostgreSQL but it doesn't work as I've planned. I have two questions:
- Is there a debugger for PostgreSQL functions?
- Where am I wrong? I need to check
idprodhere.
CREATE OR REPLACE FUNCTION add_towh() RETURNS TRIGGER AS $$
DECLARE
idsupp integer;
idprod integer;
whamount integer;
BEGIN
SELECT PRODUCT INTO idsupp from SUPPLIERS where ID = NEW.SUPPLIER;
SELECT ID INTO idprod from PRODUCTS where ID = idsupp;
if (idprod > 0) then
select amount into whamount from warehouses where product = idprod;
update warehouses set amount = whamount * new.amount;
else
insert into warehouses (product,amount) values(idprod, new.amount);
end if;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER addtowh
AFTER INSERT ON orders FOR EACH ROW EXECUTE PROCEDURE add_towh ();