Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can a trigger obtain these informations ?

Status
Not open for further replies.

Borek

Programmer
Mar 19, 2003
1
0
0
FR
Hello,

I'd like to know how can a Trigger Function (written in PL/pgSQL) obtain the name of fields where OLD content is different from NEW content. Actually, I'd like to stock these name into a list variable.

Example :

I've got this table :

CREATE TABLE public.machine (
id_machine int8 NOT NULL,
str_serial_number_machine varchar(100),
str_comment text,
str_name_machine varchar(20),
id_state_machine int8,
CONSTRAINT machine_pkey PRIMARY KEY (id_machine)
) WITH OIDS;

And i'd like to write a PL/pgSQL Trigger function which can know what are the fields that have been modified in order to obtain something like this :

CREATE FUNCTION public.trig_machine() RETURNS trigger AS 'DECLARE
req varchar(4000);
colonne RECORD;

BEGIN

-- Here's a SQL request which obtain the list of all column name of the modified table (TG_relname)

req:=''select attname from pg_attribute where attrelid=(select relfilenode from pg_class where relname='''''';
req:=req || TG_RELNAME;
req := req || '''''') and attnum>0'';

FOR colonne IN EXECUTE req LOOP
insert into machine_history (str_old_value,str_new_value) values (OLD.colonne.attnum, NEW.colonne.attnum);
END LOOP;

return NEW;

END;' LANGUAGE 'plpgsql' VOLATILE;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top