Friday, September 26, 2008

[GENERAL] dynamic sql in PL-Pgsql ?

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

 

 

 

 

 

 

 

No comments: