Search Postgresql Archives

Re: Passing a table as parameter

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

 



2011/3/21 Vibhor Kumar <vibhor.kumar@xxxxxxxxxxxxxxxx>:
>
> On Mar 21, 2011, at 11:13 PM, Jon Smark wrote:
>
>>
>> Is there any way for a SQL or PL/pgSQL function to receive a table
>> as parameter? ÂAs an illustration, consider the dummy example below.
>> Note that functions get_from_data1 and get_from_data2 follow essentially
>> the same pattern; it would be nice to define instead a single polymorphic
>> function parameterised on the id and table. ÂIs this possible?
>
>
> Try Something like given below:
> CREATE OR REPLACE FUNCTION tab_pass(text) returns setof numeric
> as
> $$
> Â DECLARE
> Â Â rec record;
> Â BEGIN
> Â Â Âfor rec in execute 'select empno from '||$1||' where id = 1'
> Â Â ÂLOOP
> Â Â Â Â Âreturn next rec.empno;
> Â Â ÂEND LOOP;
> Â Â Âreturn next rec.empno;
> Â END;
> $$ language plpgsql;

it can work too, but there is sql injection risk.

Do newer 'SELECT ... FROM ' || tabname || ' ...

Regards

Pavel Stehule

>
>
> Thanks & Regards,
> Vibhor Kumar
> EnterpriseDB Corporation
> The Enterprise PostgreSQL Company
> vibhor.kumar@xxxxxxxxxxxxxxxx
> Blog:http://vibhork.blogspot.com
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[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