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];
Laurenz Albe
61.9k4 gold badges57 silver badges93 bronze badges
asked Nov 16, 2019 at 13:17
-
This is not possible. May be you could explain the problem you're trying to solve, instead of your impossible solution to that problem.mustaccio– mustaccio2019年11月16日 14:12:22 +00:00Commented Nov 16, 2019 at 14:12
1 Answer 1
You have to use dynamic SQL like this:
EXECUTE format('SELECT 1ドル.%I', some_key) INTO value USING NEW;
answered Nov 18, 2019 at 7:21
-
Your solution is correct, thank you very much. I managed to solve the problem. See part of the code.
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;Manoel Adriano– Manoel Adriano2019年11月19日 10:22:39 +00:00Commented Nov 19, 2019 at 10:22 -
1If the answer is correct, you might accept it. Concatenating column names with
||
is a bad idea because of the risk of SQL injection. Use theformat
function like I did in my answer.Laurenz Albe– Laurenz Albe2019年11月19日 10:30:32 +00:00Commented Nov 19, 2019 at 10:30
lang-sql