Text Size: Normal / Large

pg_select

Name

pg_select -- loop over the result of a query

Synopsis

pg_select conn commandString arrayVar procedure

Description

pg_select submits a query (SELECT statement) to the PostgreSQL server and executes a given chunk of code for each row in the result. The commandString must be a SELECT statement; anything else returns an error. The arrayVar variable is an array name used in the loop. For each row, arrayVar is filled in with the row values, using the column names as the array indices. Then the procedure is executed.

In addition to the column values, the following special entries are made in the array:

.headers

A list of the column names returned by the query.

.numcols

The number of columns returned by the query.

.tupno

The current row number, starting at zero and incrementing for each iteration of the loop body.

Arguments

conn

The handle of the connection on which to execute the query.

commandString

The SQL query to execute.

arrayVar

An array variable for returned rows.

procedure

The procedure to run for each returned row.

Return Value

None

Examples

This examples assumes that the table table1 has columns control and name (and perhaps others):

pg_select $pgconn "SELECT * FROM table1;" array {
    puts [format "%5d %s" $array(control) $array(name)]
}


User Comments

No comments could be found for this page.

Add Comment

Please use this form to add your own comments regarding your experience with particular features of PostgreSQL, clarifications of the documentation, or hints for other users. Please note, this is not a support forum, and your IP address will be logged. If you have a question or need help, please see the faq, try a mailing list, or join us on IRC. Note that submissions containing URLs or other keywords commonly found in 'spam' comments may be silently discarded. Please contact the webmaster if you think this is happening to you in error.

In order to submit a comment, you must have a community account.

* Comment
 

* denotes required field

Privacy Policy | Project hosted by hub.org | Designed by tinysofa
Copyright © 1996 – 2007 PostgreSQL Global Development Group