0

I've tried this trigger using PgAdmin4; (this GUI i think have some bugs)

my trigger is:

CREATE TRIGGER name_update BEFORE UPDATE ON customer
    FOR EACH ROW
    BEGIN
          SET NEW.name = CONCAT_WS(', ', NEW.lastname, NEW.firstname);
    END;

 CREATE TRIGGER name_insert BEFORE INSERT ON customer
    FOR EACH ROW
    BEGIN
        SET NEW.name = CONCAT_WS(', ', NEW.lastname, NEW.firstname);
    END;

but i'm getting this error:

i've tried remove begin and end, but nothings happens, maybe must I to try on the shell, or in txt file? what is the best way to do it outside pgadmin and manage the carriage returns, in mysql i've used to use delimeter on the shell.

DDave
  • 1,400
  • 3
  • 16
  • 33
  • Where [in the manual](https://www.postgresql.org/docs/current/static/plpgsql-trigger.html) did you find that syntax? –  Nov 09 '17 at 11:20
  • here on https://stackoverflow.com/questions/15445944/how-to-create-trigger-to-do-concatenation-of-fields and here: https://stackoverflow.com/questions/28732963/how-to-auto-populate-a-name-based-on-first-and-last-name-in-mysql – DDave Nov 09 '17 at 11:36
  • manual had no practical examples for my case – DDave Nov 09 '17 at 11:38

1 Answers1

0

You need create procedure and call her in trigger. Example:

create or replace function trigger_function()
returns trigger as
$$
begin
    NEW.name = CONCAT_WS(', ', NEW.lastname, NEW.firstname);
    return NEW;
end;
$$
language plpgsql;

CREATE TRIGGER name_update
  BEFORE UPDATE ON customer
  FOR EACH ROW
  EXECUTE PROCEDURE trigger_function();
svm
  • 392
  • 3
  • 9