Re: Cycle thru columns in table?

From: Rich <richmarin_at_earthlink.net>
Date: Fri, 5 Mar 2010 13:14:31 -0800 (PST)
Message-ID: <68bec664-0d1c-4a5a-90c0-1097df8cbccb_at_b30g2000yqd.googlegroups.com>



On Mar 2, 4:57�pm, "Gerard H. Pille" <g..._at_skynet.be> wrote:
> Rich wrote:
>
> > 1 am using 10g.
>
> > I need code that returns the column name of the table and the value of
> > a row of code. For example, assume I have a table called Customer.
> > Then imagine if there is a column called First_Name. My SQL will
> > return one record. So that the output might look like
>
> > First_Name = Fred
>
> > The psudo code might look like
>
> > For i = 1 to the last column in table
> > � � column_name := column(i)
> > � � record_value := cursor(i)
> > Loop
>
> > How can I do the above in Oracle?
>
> An example using PHP, good for any database, I guess. �The column names are used as column
> headers. �You can drop the rowcount and bail out after the first row.
>
> � � � � $tables =
> � � � � � array( "users", "machines", "games", "runs_on", "owns", "likes");
>
> � � � � // loop over tables
> � � � � foreach( $tables as $table ) {
> � � � � � // loop over rows of database table
> � � � � � $sql = "SELECT count(*) aantal FROM $table";
> � � � � � foreach ($db->query($sql) as $row) {
> � � � � � � $rowcount = $row[0];
> � � � � � � print "$table contains $row[0] row(s)<br>";
> � � � � � }
>
> � � � � � // print contents if any
> � � � � � if ($rowcount > 0) {
> � � � � � � $sql = "SELECT * FROM $table";
> � � � � � � $stmt = $db->query($sql);
> � � � � � � print "<table><tr>";
>
> � � � � � � // show column names
> � � � � � � for ($i = 0; $i < $stmt->columnCount(); $i++) {
> � � � � � � � $cmeta = $stmt->getColumnMeta($i);
> � � � � � � � print "<th>" . $cmeta['name'];
> � � � � � � }
> � � � � � � print "</tr>";
>
> � � � � � � // print the rows
> � � � � � � foreach ($stmt as $row) {
> � � � � � � � print "<tr>";
> � � � � � � � // print column by column
> � � � � � � � for ($i = 0; $i < $stmt->columnCount(); $i++) {
> � � � � � � � � print "<TD>" . $row[$i];
> � � � � � � � }
> � � � � � � � print "</tr>";
> � � � � � � }
> � � � � � � print "</table>";
> � � � � � }
> � � � � }- Hide quoted text -
>
> - Show quoted text -

Thanks for the response.

Is this possible in PL/SQL? Received on Fri Mar 05 2010 - 15:14:31 CST

Original text of this message