Search Postgresql Archives

Re: dynamic plpgsql question

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

 



Marc Evans wrote:
>>> How can I get the value of NEW.{column_name} (aka NEW.magic in this
>>> specific test case) into the variable data?
>> EXECUTE 'SELECT ' || NEW.column_name ';' INTO data;
> 
> Thanks for the suggestion. Unfortunately, it does not work:
> 
> CREATE OR REPLACE FUNCTION foo() RETURNS TRIGGER AS $$
>   DECLARE
>     column_name TEXT := TG_ARGV[0];
>     data TEXT;
>   BEGIN
>     EXECUTE 'SELECT ' || NEW.column_name || ';' INTO date;
>     -- ...
>   END;
> $$ LANGUAGE plpgsql;
> 
> c3i=> insert into test_table values (1,1);
> ERROR:  record "new" has no field "column_name"

Writing an SP that fetches an array or a text string with column names
given a table name as input is not that hard to write.

Simply fetch "SELECT column_name FROM information_schema.columns WHERE
schema_name = $1 AND table_name = $2" into an array and use
array_to_string() or some such to make it a comma separated string of
columns. You can use that string in your dynamic query.

Cheers.
-- 
Alban Hertroys
alban@xxxxxxxxxxxxxxxxx

magproductions b.v.

T: ++31(0)534346874
F: ++31(0)534346876
M:
I: www.magproductions.nl
A: Postbus 416
   7500 AK Enschede

// Integrate Your World //


[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