Search Postgresql Archives

Re: INSERT INTO from a SELECT query

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
      choose an index scan if your joining column's datatypes do not
      match

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux