M. P.
M. P.

Reputation: 1

update column with trigger in postgres

I have this code for my table asf with column price and a column category:

CREATE OR REPLACE FUNCTION aft_update()
  RETURNS trigger AS
$$
BEGIN
UPDATE asf SET price = new.price='300' WHERE category = 'pro';
RETURN NEW;
END;

$$
LANGUAGE 'plpgsql';

CREATE TRIGGER updt_log
  AFTER UPDATE
  ON asf
  FOR EACH ROW
  EXECUTE PROCEDURE aft_update();

but for some reason even though the trigger runs successfully, the update does not work! I want to update multiple rows.

Upvotes: 0

Views: 111

Answers (1)

nbk
nbk

Reputation: 49373

For that you don't need any trigger or function at all

CREATE TABLE asf (id serial, price DECIMAL(10,2),category varchar(10))
INSERT INTO asf (price,category) VALUES (10.2,'pro'),(11.2,'pro')
UPDATE asf SET price = 300 where category = 'pro'

2 rows affected

SELECT * FROM asf
id |  price | category
-: | -----: | :-------
 1 | 300.00 | pro     
 2 | 300.00 | pro     

db<>fiddle here

Upvotes: 1

Related Questions