Re: Column headings question

Michael Peppler <[email protected]> Wed, 7 Oct 2009 20:18:31 +0200
Newsgroups gmane.comp.lang.perl.modules.dbi.sybase
Message-ID <[email protected]>
On Oct 7, 2009, at 5:39 PM, Louis Proyect wrote:

> Is there an easier way to access column headings than this:
>
> @res=$dbh->ct_sql("exec stored_procedure");
>
> @res2=$dbh->ct_sql("sp_help table_name"); ---> this is the table that
> maps to the stored procedure results
>
> $cntr = 0;
>
> foreach $row (@res2)   # Loop through each row
> {
>  print "@$row[0]: ";
>  print "$res[0][$cntr]\n";
>  $cntr++;
> }
>
> Most importantly, this only works if the stored procedure is returning
> results that map to a single table. If the results are from joined
> tables, you are out of luck.
>

You can use the optional doAssoc flag to have ct_sql return an array  
of hash references, where each row is keyed on the column names.

Something like

@res = $dbh->ct_sql($cmd, undef, 1);
foreach my $row (@res) {
    foreach my $col (keys(%$row)) {
	print "$col: $row->{$col}\n";
    }
}

Michael
--
Michael Peppler
Sybase on Linux FAQ: http://www.peppler.org/FAQ/linux.html

"A successful [software] tool is one that was used to do something  
undreamed of by its author." -- S. C. Johnson