Function PostgreSQL

0

I need to create a Function() in my database PostgreSQL so that every time a INSERT or UPDATE is moved in a given table, I activate that function through a TRIGGER so that in my table of contents log is stored all the movements that the product has undergone. I created the following Function :

CREATE OR REPLACE FUNCTION public.atualiza_suprimentoslog()
RETURNS trigger
AS
$$
begin
    IF (TG_OP = 'UPDATE') then
        IF (NEW.numeroserie <> OLD.numeroserie) THEN
            INSERT INTO suprimentoslog (codigoestoque, numeroserie, dataoperacao, clienteempresa, clienteusuario, solicitante, operacao)
            values
            (new.codigoestoque, new.numeroserie, current_date, ' - ', ' - ', ' - ', 'ALTERACAO SUPRIMENTO') 
            return new;
        END IF;
    end if;
    if (TG_OP = 'INSERT') then
        INSERT INTO suprimentoslog (codigoestoque, numeroserie, dataoperacao, clienteempresa, clienteusuario, solicitante, operacao)
        values
        (new.codigoestoque, new.numeroserie, current_date, ' - ', ' - ', ' - ', 'ENTRADA SUPRIMENTO') 
        return new;
    end if;
    return null;
end
$$  LANGUAGE plpgsql

But it returns the following error when I try to execute it:

ERROR:  syntax error at or near "return"
LINE 11:    return new;
            ^
********** Error **********

ERROR: syntax error at or near "return"
SQL state: 42601
Character: 411
    
asked by anonymous 08.09.2017 / 18:51

1 answer

1

The semicolon is missing at the end of INSERT . Ex:

INSERT INTO suprimentoslog (
codigoestoque
, numeroserie
, dataoperacao
, clienteempresa
, clienteusuario
, solicitante
, operacao)
values
(new.codigoestoque
, new.numeroserie
, current_date
, ' - '
, ' - '
, ' - '
, 'ALTERACAO SUPRIMENTO'); --Aqui
    
08.09.2017 / 19:16