On Mon, Sep 09, 2002 at 05:03:48PM -0400, Kyle wrote:
> Hello
>
> How can I get SQL to return the column names when doing a select using
> the DBI?
>
> Why do I need this? I am trying to make one sub that will do my various
> selects. Right now I'm doing it with discrete subs. I find that these
> subs are EXACTLY the same except for parsing the SQL returns by the
> appropriate column name.
>
> Here is a sample of what I'm trying to do (3 requests to the same sub):
>
> @ArrayOfHashes = sub_get_sql('SELECT * FROM table WHERE trxnum = 100;');
>
> @ArrayOfHashes = sub_get_sql("SELECT trxdate FROM table WHERE trxowner =
> \'kyle\';");
>
> @ArrayOfHashes = sub_get_sql("SELECT trxnum, ordernum, trxowner,
> trxinfo, trxdate FROM table WHERE trxnum = \'$trxnum\'");
>
> Notice that the columns returned to the sub are different by column
> order and the number of columns returned. I can handle the perl parsing
> part, but the sub needs to know the column names when they come back.
Try using one of the hashref methods to fetch the data, such as
fetchrow_hashref(). Alternatively, you can use the NAME_lc or NAME_uc
attributes of a statment handle, e.g. $sth->{'NAME_lc'} is a ref to an
array of lowercase column names returned by the query.
See the DBI documentation for more information on these approaches.
Ronald