Postgres- SQL 状态:22004 - EXECUTE 的查询字符串参数为空

Postgres- SQL state: 22004 - query string argument of EXECUTE is null

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

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

vgi_match_id_col, vgi_match_table_col, vgi_table, output_table 

函数代码如下所示,ro用于保存第一个table查询,match_row保存查询外部table的输出。距离是使用 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;

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

Similar Question

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

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

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

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

例如

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

在 postgres 中,如果在动态 DML 中传递任何 null,我们一定会遇到这个问题。"query string argument of EXECUTE is null" 您可以使用以下示例步骤进行插入和更新。

CREATE OR REPLACE FUNCTION samplefunc(

    col1param character varying,
    col2param character varying,
    col3param character varying,
    col4param character varying,
    col5param character varying,
    col6param character varying,
    col7param character varying,
    col8param character varying

    RETURNS boolean AS

$BODY$

declare

begin

EXECUTE format( 'insert into '||tablename||' (id, col1, col2, col3, col4, col5)values(,,,,)') USING  col1param ,col2param,col3param,col4param,col5param;


EXECUTE format('update '||tablename||' set  col1 =,col2 = ,col3=,col4=,col5=
where col6= and col7= and col8= ') using col1param,col2param,,col3param,col4param,col5param,col6param,col7param,col8param;

end