Thread: postgresql and perl?

postgresql and perl?

From
Peter Landis
Date:
Hi-
   I'm a newbie at postgresql and was working on
sorting by category.  What my question is, how do you
sort by category when using a variable.  For instance,
you can sort by name in perl by doing:

$sqh = $dbh->prepare(q{select name from company order
by name;});
$sqh->execute();

but what if you have a variable set like:
$sort_selection = name;

How do you sort by the variable?

For instance you cannot do:
$sqh = $dbh->prepare(q{select name from company order
by ?;});
$sqh->execute($sort_selection);

OR

$sqh = $dbh->prepare(q{select name from company order
by $sort_selection;});
$sqh->execute();

If anyone could help, I would greatly appreciate it.

Thanks again!




__________________________________________________
Do You Yahoo!?
Kick off your party with Yahoo! Invites.
http://invites.yahoo.com/

Re: postgresql and perl?

From
Herbert Liechti
Date:
Peter Landis wrote:
>
>
> How do you sort by the variable?
>
> $sqh = $dbh->prepare(q{select name from company order
> by $sort_selection;});
> $sqh->execute();

Your problem is not Perl specific. The order by column
must be in the result set (projection). Why not doing
it this way:

my @fields = qw/name zip location/;
push @fields, $sort_selection;

$sqh = $dbh->prepare( "SELECT " . join(', ', @fields) .
     " FROM company ORDER BY $sort_selction; );
$sqh->execute();

Greetings Herbie
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herbert Liechti                     E-Mail: Herbert.Liechti@thinx.ch
ThinX networked business services        Stahlrain 10, CH-5200 Brugg
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~