Skip site navigation (1) Skip section navigation (2)

dynamic sql in PL-Pgsql ?

From: "Gauthier, Dave" <dave(dot)gauthier(at)intel(dot)com>
To: <pgsql-general(at)postgresql(dot)org>
Subject: dynamic sql in PL-Pgsql ?
Date: 2008-09-26 20:19:33
Message-ID: 0836165E8EE50F40A3DD8F0D87137267012158BE@azsmsx421.amr.corp.intel.com (view raw or flat)
Thread:
Lists: pgsql-general
Hi:

 

I want to do something like this.....

 

 

create or replace function foo(varchar, varchar) returns integer as $$

declare

  user_attrib   alias for $1;

  attrib_val    alias for $2;

  rec record;

 

begin

  

  for rec in

    select name from people_table where user_attrib = attrib_value

  loop 

     insert into my_temp_table (attrib, value, name) values
(user_attrib, attrib_value, rec.name);

  end loop;

 

end;

$$ language plpgsql;

 

As you can see, the select statement needs to be dynamic in that the
column name is stored in a variable. 

 

Can this be done?

 

Thanks

-dave

 

 

 

 

 

 

 

Responses

pgsql-general by date

Next:From: Raymond O'DonnellDate: 2008-09-26 20:38:58
Subject: Re: dynamic sql in PL-Pgsql ?
Previous:From: Decibel!Date: 2008-09-26 18:45:02
Subject: Is there any way to reliably influence WHERE predicate evaluation ordering?

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group