0

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.

5
  • what Do you try to do with SET price = new.price='300' Commented Jun 25, 2021 at 21:33
  • I already have a price filled in the column price (for example 400) and I want to change all prices under category pro to 300 Commented Jun 25, 2021 at 21:34
  • Why do you want to change rows that are not affected by the UPDATE? Commented Jun 25, 2021 at 21:39
  • 1
    your query doesn't nake sense you have a trigger oin asf and write also a log for it in the same table? this should be another table Commented Jun 25, 2021 at 21:41
  • I want to change the price column, not the category one. I only use the category column to specify where I want the change to be Commented Jun 25, 2021 at 21:41

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I know a trigger is not necessary but I was trying to use one and cant figure out why it's not functioning!
so you trigger the trigger after an update and the function updates which triggers the after update trigger and so on and so on

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.