我有一个表(名为 VGI_table),其中包含一个列(名为 match_tabl),其中包含同一数据库中其他表的名称以及这些表的 object_ids。我正在尝试创建一个 plpgsql 函数,该函数遍历 VGI_table 中的每一行并执行查询以从另一个表中检索对象,如下所示。
该函数有 4 个参数(都是 varchar),前两个是 VGI_table 中的列名,第三个是 VGI_table 的名称,最后一个参数是输出。
vgi_match_id_col, vgi_match_table_col, vgi_table, output_table
Run Code Online (Sandbox Code Playgroud)
该函数的代码如下所示,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||' …Run Code Online (Sandbox Code Playgroud)