【问题标题】:Postgres- SQL state: 22004 - query string argument of EXECUTE is nullPostgres- SQL 状态:22004 - EXECUTE 的查询字符串参数为空
【发布时间】:2016-02-12 05:36:37
【问题描述】:

我有一个表(名为 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

【问题讨论】:

    标签: postgresql postgis plpgsql dynamic-sql execute


    【解决方案1】:

    嗯,显然你要连接的东西是空的。

    请改用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);
    

    【讨论】:

    • 感谢您的快速响应和帮助。它实际上是包含 NULL 输出的距离函数,它是引发错误的第二次执行。包括 USING 函数转义了 NULL 值。再次感谢您的帮助。
    【解决方案2】:

    在 postgres 中,如果在 Dynamic DML 中传递了任何空值,我们一定会遇到这个问题。“EXECUTE 的查询字符串参数为空” 您可以使用以下示例步骤进行插入和更新。

    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($1,$2,$3,$4,$5)') USING  col1param ,col2param,col3param,col4param,col5param;
    
    
    EXECUTE format('update '||tablename||' set  col1 =$1,col2 = $2,col3=$3,col4=$4,col5=$5
    where col6=$6 and col7=$7 and col8=$8 ') using col1param,col2param,,col3param,col4param,col5param,col6param,col7param,col8param;
    
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-10
      • 2015-02-02
      相关资源
      最近更新 更多