本文介绍了PostgreSQL plpgsql-变量列名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在创建一个触发器,该触发器为列使用动态名称
I am creating a trigger, which uses dynamic names for columns
NEW.name:=2222; -- works fine !但是
dynamic_column:='name'; EXECUTE '$1.'||dynamic_column||':=2222 ' USING NEW; -- raises error出现错误:
错误:"$ 1"或附近的语法错误LINE 1:$ 1.name:= 2222
ERROR: syntax error at or near "$1" LINE 1: $1.name:=2222
推荐答案
我在这里找到了信息:通过键入Postgres触发器分配为NEW
I found info here: Assign to NEW by key in a Postgres trigger
如果我们通过以下方式启用hstore模块:
If we enable the module hstore by:
CREATE EXTENSION hstore;我们可以这样做:
dynamic_column:='name'; temp_sql_string:='"'||dynamic_column||'"=>"2222"'; NEW := NEW #= temp_sql_string::hstore;现在将RECORD NEW.name设置为值2222.
And the RECORD NEW.name now is set to the value 2222.
感谢您努力寻找解决方案@Laurenz Albe
Thank you tough for making an effort to find a solution @Laurenz Albe
更多推荐
PostgreSQL plpgsql
发布评论