Re: get column name passed to a function

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To:
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: get column name passed to a function
Date: 2012-11-20 22:06:54
Message-ID: 50ABFEFE.2060804@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/20/2012 02:03 PM, Adrian Klaver wrote:
> On 11/20/2012 12:51 PM, Rob Sargent wrote:
>> On 11/20/2012 01:35 PM, Adrian Klaver wrote:
>>> On 11/20/2012 11:36 AM, Rhys A.D. Stewart wrote:
>>>> Greetings,
>>>>
>>>> I'm looking to obtain the name of a column that is passed to a function,
>>>> similar to xmlforest. Suggestions?
>>>
>>> This is going to require some more information.
>>>
>>> 1) What language is the function written in?
>>>
>>> 2) Trigger function or not?
>>>
>>> 3) A sample of the code?
>>>
>>>>
>>>> Regards,
>>>>
>>>>
>>>> Rhys
>>>
>>>
>>
>> Is this along the right line(s)?
>>
>>
>> create or replace function f(cname text) returns table(c text)
>> as
>> $$
>> declare
>> v text;
>> begin
>> select 'hello' into v;
>> raise notice 'Column name is %', v;
>> execute 'select ' || v || ' as ' || cname;
>> end;
>> $$ language plpgsql;
>
> So what you are looking to do is build dynamic queries?
>
> If so take a look at:
>
> http://www.postgresql.org/docs/9.2/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
>
> In particular:
>
> Example 39-1. Quoting Values In Dynamic Queries
>
>>
>>
>>
>
>

A correction to earlier

create or replace function f(cname text) returns table(c text)
as
$$
declare
v text;
begin
raise notice 'Column name is %', cname;
execute 'select quote_literal(hello) ' || ' as ' || cname;
end;
$$ language plpgsql;

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jeff Janes 2012-11-20 22:08:17 Re: High SYS CPU - need advise
Previous Message Gary Chambers 2012-11-20 21:53:42 Re: timezone, how postgres deal with it?