SQLize Online / PHPize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
CREATE TABLE currencies ( currency char(3), rate numeric(9, 2) ); CREATE OR REPLACE FUNCTION update_product_prices() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF OLD.rate IS DISTINCT FROM NEW.rate THEN CASE NEW.currency WHEN 'KZT' THEN UPDATE products SET price_kzt = price * NEW.rate; WHEN 'BYN' THEN UPDATE products SET price_kzt = price * NEW.rate; END CASE; END IF; END; $$; CREATE OR REPLACE TRIGGER update_price AFTER UPDATE OR INSERT ON currencies FOR EACH ROW EXECUTE FUNCTION update_product_prices();

Stuck with a problem? Got Error? Ask ChatGPT!

Copy Clear