-1

I need create this query dynamically:

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);

The only thing that changes is:

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

For:

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

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

I hope to be clear. And if possible tutorials to learn PL/pgSQL. Thanks.

1
  • The best pl/pgsql tutorial is the Postgres docs itself... Commented May 29, 2013 at 14:52

1 Answer 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 only works inside a plpgsql function. I'm supposing you already have a working function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.