Re: To pass schemaname as a function parameter - Mailing list pgsql-general

From Merlin Moncure
Subject Re: To pass schemaname as a function parameter
Date
Msg-id b42b73150909030439ld9808day7840971a673f3fe7@mail.gmail.com
Whole thread Raw
In response to Re: To pass schemaname as a function parameter  ("A. Kretschmer" <andreas.kretschmer@schollglas.com>)
List pgsql-general
On Thu, Sep 3, 2009 at 7:37 AM, A.
Kretschmer<andreas.kretschmer@schollglas.com> wrote:
> In response to Kalai R :
>> Hi,
>>
>> I wrote a function in plpgsql, to dispaly the student list.
>>
>> In a Database all schemas contains studentlist table. so I wrote the function with schemaname as a parameter(text
datatype). My code is like 
>>
>> CREATE FUNCTION disp_fn(schemaname text) AS $$
>> BEGIN
>>     SELECT * FROM schemaname.studentlist;
>> END;
>> $$ LANGUAGE plpgsql;
>
> Not possible in this way, use EXECUTE:
>
>   execute 'select * from ' || schemaname || '.studentlist';
>
>
> (it is a dynamic SQL, you haven't a fix tablename)

also (IMO preferred),

execute 'set search_path = public, ' || schemaname;
SELECT * FROM studentlist;

merlin

pgsql-general by date:

Previous
From: "A. Kretschmer"
Date:
Subject: Re: To pass schemaname as a function parameter
Next
From: Tim Landscheidt
Date:
Subject: Re: maximum count of contiguous years