Re: INSERT INTO from a SELECT query - Mailing list pgsql-general

From Adam O'Toole
Subject Re: INSERT INTO from a SELECT query
Date
Msg-id BAY102-F25E5A15CD7AA297D185641FBDE0@phx.gbl
Whole thread Raw
In response to Re: INSERT INTO from a SELECT query  (Tino Wildenhain <tino@wildenhain.de>)
List pgsql-general
I solved it. The statment worked as is, I just had to use dynamic SQL (put
the statement in a string and the EXECUTE the string). Here is what I did:

CREATE FUNCTION test(varchar)  RETURNS int2 AS'
DECLARE
   id_list ALIAS FOR $1;
   query varchar;
BEGIN
   query := '' INSERT INTO history (media_id, media_type) SELECT
media.media_id, media.media_type WHERE media.media_id IN ( '' || id_list ||
'')'';

   EXECUTE query;

Now I can call this function like this
  SELECT test( '24,25,26,27,28,29' );
and it will execute the INSERT statement for each value in the passed
varchar


-Thanks guys for your input

Adam



pgsql-general by date:

Previous
From: Karsten Hilbert
Date:
Subject: chosing a database name
Next
From: "John Tulodziecki"
Date:
Subject: Client-Server Example