-1

我需要动态创建这个查询:

INSERT INTO "calle" (a, b, c, d)
SELECT l.id_localidad, v.tipovial, v.nomvial, v.geom
  FROM "010010001v" AS v,
dblink('dbname=xxx port=xxxx host=xxxxx user=xxx password=xxxxx',
    'SELECT id_localidad
           FROM "localidadesurbanas"
 WHERE cvgeo = ''010010001''')
  AS l(id_localidad integer);

唯一改变的是:

...
FROM "010010001v"
...
WHERE cvgeo = ''010010001''')
...

为了:

SELECT tablename
  FROM pg_tables
 WHERE schemaname = 'public'
   AND tablename LIKE '01%v';

"010010001v"
"010010293v"
"010010479v"
...

我希望清楚。如果可能的话,还有学习 PL/pgSQL 的教程。谢谢。

4

1 回答 1

0
execute format ($dynamic$
    INSERT INTO "calle" (a, b, c, d)
    SELECT l.id_localidad, v.tipovial, v.nomvial, v.geom
    FROM
        %I AS v,
        dblink(
            'dbname=xxx port=xxxx host=xxxxx user=xxx password=xxxxx',
            '
                SELECT id_localidad
                FROM "localidadesurbanas"
                WHERE cvgeo = $1
            '
        ) AS l(id_localidad integer);
    $dynamic$, '010010001v'
) using '010010001'

execute仅适用于 plpgsql 函数。我假设你已经有一个工作功能。

于 2013-05-29T15:57:56.817 回答