postgresql - Postgres- SQL 状态 : 22004 - query string argument of EXECUTE is null

标签 postgresql postgis plpgsql dynamic-sql execute

我有一个表(名为 VGI_table),其中包含一个列(名为 match_tabl),该列包含同一数据库中其他表的名称以及这些表的 object_id。我正在尝试创建一个 plpgsql 函数,它循环遍历 VGI_table 中的每一行并执行查询以从另一个表中检索对象,如下所示。

该函数有 4 个参数(均为 varchar),前两个是 VGI_table 中列的名称,第三个是 VGI_table 的名称,最后一个参数是输出。

vgi_match_id_col, vgi_match_table_col, vgi_table, output_table 

函数的代码如下所示,ro用于保存第一个表查询,match_row保存查询到的外部表的输出。距离是使用 PostGIS st_distance 函数创建的输出。

DECLARE
   ro record;
   match_row record;
   distance float; 

BEGIN

for ro in EXECUTE 'select gid, geom, '||vgi_match_id_col||' as match_id, '||vgi_match_table_col||' as match_table from '||vgi_table
LOOP
    --raise notice '(%)', 'select geom from public.'||ro.match_table||' where gid = '||ro.match_id;


    execute 'select geom from public.'||ro.match_table||' where gid = '||ro.match_id into match_row;


    distance := st_distance(ro.geom, st_transform(match_row.geom,st_srid(ro.geom)));
    EXECUTE 'INSERT INTO '||output_table||' VALUES('||ro.gid||', '||distance||')';


END LOOP;

正在查询的表在 match_tabl 列或 object_id 列中没有空值。当尝试执行 EXECUTE 语句时,代码将 ro.match_table 和 ro.match_id 标识为空值。我什至将 RAISE NOTICE 函数与 EXECUTE 语句中使用的相同字符串一起使用,并返回了正确的查询。如果我使用预定义的 table_name 和对象 ID 对执行字符串进行硬编码,则脚本可以正常工作。下面的链接类似,但我认为它没有解决我的问题。感谢您的帮助。

Similar Question

最佳答案

好吧,很明显你连接的东西是空的。

改用format 函数,这样您将获得更多有用的信息。

format('select geom from public.%I ....', ro.match_table);

使用 EXECUTE ... USING ... 插入文字。

例如

EXECUTE format('INSERT INTO %I VALUES($1, $2)', output_table) USING (ro.gid, distance);

关于postgresql - Postgres- SQL 状态 : 22004 - query string argument of EXECUTE is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33645292/

相关文章:

sql - 错误 : Unable to replace text string in PostgreSQL

postgresql - osm2pgsql delete_node 失败

Postgresql - 如何在一个表中获取在另一个表中没有匹配项的条目

python - Django QuerySet 与原始 SQL 性能注意事项

postgresql - 无法使用 psycopg2 截断表

python - SQLAlchemy:PostGIS 的 create_engine() 语法错误

c# - Npgsql 参数化查询输出与 PostGIS 不兼容

sql - Postgresql plpgsql/sql 是否支持 where 子句中的短路?

sql - Postgresql:从存储过程返回临时表

postgresql - plpgsql 循环查询和更新