Search code examples
postgresqlsyntax-error

Syntax error in the code creating a PostgreSQL procedure


CREATE OR REPLACE FUNCTION 
    Cree_RefFacture() 
RETURNS trigger 
AS 
$BODY$ 
    BEGIN 
        NEW."RefFacture" := nextval("dbo.Base_Factures_RefFacture_seq"); 
        RETURN NEW; 
    END; 
$BODY$ 
LANGUAGE plpgsql;

I get syntax error on this line :

Syntax error on or near “NEW” 
LINE 7:NEW."RefFacture":=nextval"dbo.Base_Factures_RefFacture_seq");

Something wrong in your opinion?

I've tried various solutions, I keep getting the same error. Could you help me please


Solution

  • There is no need for a trigger and trigger function to get an id for a primary key. Just use an identity column and you will be fine.

    Example:

    CREATE TABLE t1(
        id_t1 INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
        content TEXT
    );
    
    INSERT INTO t1(content)
    VALUES ('my content'), ('some other content')
    RETURNING id_t1;
    
    SELECT * FROM t1;