In the trigger body, how can I get a value of NEW
by the field name?
This is what I want to do:
some_key = "some_column";
value := NEW[some_key];
In the trigger body, how can I get a value of NEW
by the field name?
This is what I want to do:
some_key = "some_column";
value := NEW[some_key];
You have to use dynamic SQL like this:
EXECUTE format('SELECT $1.%I', some_key) INTO value USING NEW;
code
FOR ri IN SELECT ordinal_position, column_name, data_type FROM information_schema.columns WHERE table_schema = quote_ident(TG_TABLE_SCHEMA) AND table_name = quote_ident(TG_TABLE_NAME) AND column_name = quote_ident(nmpk) ORDER BY ordinal_position LOOP EXECUTE 'SELECT ($1).' || ri.column_name || '::text' INTO t USING NEW; idValue := t; END LOOP;
Nov 19, 2019 at 10:22
||
is a bad idea because of the risk of SQL injection. Use the format
function like I did in my answer.
Nov 19, 2019 at 10:30