On Wed, Sep 28, 2005 at 12:25:54PM -0700, vishal saberwal wrote:
> I am trying to create a stored procedure that takes a column name as
> parameter and then uses it to sort the table for result set.
> create or replace function ptest_Sort_Select(varchar) returns setof ptest1
> as $$
> res ptest1%ROWTYPE;
> for res in
> select * from ptest1 order by ptest1.$1 asc
> return next res;
> end loop;
> $$ language plpgsql strict;
But you might try
for res in execute 'select * from ptest1 order by ' || $1 || ' asc'
i.e. build up the query string and run with execute.
> but the output was not sorted.
> Then i tried this stored procedure:
> create or replace function test(varchar) returns int as $$
> res int;
> raise notice 'select * from ptest1 order by $1 asc';
I don't think plpgsql expands variables within strings. You'll have
to concatenate yourself, like so:
raise notice 'select * from ptest1 order by ' || $1 || ' asc';
In response to
pgsql-bugs by date
|Next:||From: Andrew||Date: 2005-09-29 08:27:44|
|Subject: BUG #1920: Installer no WIN1252 & UTF8 selection|
|Previous:||From: Tom Lane||Date: 2005-09-28 21:23:23|
|Subject: Re: BUG #1916: selection criteria from one outer join on clause applied to other joins |