postgresql - Trigger to update a column during insert in postgres -


I have two tables (in postgrad) - after putting each in the ads and logs log table, depending on the action Increase the calculation of a column in the advertising table.

I have tried to write a function that will trigger the trigger, but when trying to create a function it throws an error. I am new to postgraxal triggers and functions, so someone can tell what is wrong with it.

update_ad_count returns the action_ad_count (action characters (4), ad_id INT to zero as $$ in the case of action = 'VIEW' then (updated ad SET view = (SELECT views + 1 Ad where id = ad_id), up to date = now () WHERE id = ad_id;) end; $$ Language SQL

Error I get

  Error: "case" line syntax error on line 2 or: $$ as the case when Action = \ 'VIEW \' then ^  

Update: @Tomalak: Thanks for the function and update statement update (can not be resisted by the moment) < / P>

I have learned after so many googling, that there should be no parameter in the function and should use NEW.col_name and return a TRIGGER.

First of all, your UPDATE call should be:

  Update ad SET view = visible + 1, up to date = now () WHERE id = ad_id; According to  

, something like this should be done:

  create update_ad_count (action characters (4), ad_id INT) returned as $$ BEGIN If action = 'VIEW' then updated ad SEV view = visible + 1, updated = now () WHERE id = ad_id; end if; End; $$ LANGUAGE plpgsql;  

The case is not a flow-control statement, it can not be used like an IF statement. This is a value-producing statement (an expression) and should be used in such a way, e.g. You have to select / update it.


Comments