How can I combine old / new with a variable in postgres?

0

I'm doing as a type of blog, and I'm trying to save the record id the table that triggers my function. There are two tables, customers and orders, I want that if customers shoot my function, I saved the customerid that was inserted, modified or deleted, and for this I use old or new. Even if it were with order, I want the order of the record to be saved to which something was done. For that I have this code:

BEGIN

IF(TG_TABLE_NAME = 'customers') THEN
    prim:= "customerid";
ELSIF(TG_TABLE_NAME = 'orders') THEN
    prim:= "orderid";
end if;

    IF(TG_OP = 'INSERT') THEN 
        INSERT INTO public."bitacora"("reg_id","columna","tabla")
        VALUES(new.prim,prim,TG_TABLE_NAME);
        RETURN NEW;

    ELSIF(TG_OP = 'DELETE') THEN 
        INSERT INTO public."bitacora"("reg_id","columna","tabla")
        VALUES(old.prim,prim,TG_TABLE_NAME);
        RETURN OLD;

    ELSIF(TG_OP = 'UPDATE') THEN 
        INSERT INTO public."bitacora"("reg_id","columna","tabla")
        VALUES(new.prim,prim,TG_TABLE_NAME);
        RETURN NEW;
    END IF;
END;

prim would be the name of the column within the table (either customers or orders) that has the id. I need that if it is done delete, that it is sent in old, and that if it is insert or update, send it in new

The error that throws me is that "column" customerid "does not exist" for the moment where it does the new.prim or old.prim. And the column does exist, it's totally the same, so I do not know

    
asked by ShootingWizard 16.04.2018 в 08:38
source

0 answers