<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >pg_select</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REV="MADE" HREF="mailto:pgsql-docs@postgresql.org"><LINK REL="HOME" TITLE="PostgreSQL 7.4.1 Documentation" HREF="index.html"><LINK REL="UP" TITLE="pgtcl Command Reference" HREF="libpgtcl-ref.html"><LINK REL="PREVIOUS" TITLE="pg_result" HREF="pgtcl-pgresult.html"><LINK REL="NEXT" TITLE="pg_execute" HREF="pgtcl-pgexecute.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2003-12-22T03:48:47"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="5" ALIGN="center" VALIGN="bottom" >PostgreSQL 7.4.1 Documentation</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="pgtcl-pgresult.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="10%" ALIGN="left" VALIGN="top" ><A HREF="pgtcl-pgresult.html" >Fast Backward</A ></TD ><TD WIDTH="60%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="pgtcl-pgexecute.html" >Fast Forward</A ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="top" ><A HREF="pgtcl-pgexecute.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="PGTCL-PGSELECT" ></A >pg_select</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN22614" ></A ><H2 >Name</H2 >pg_select -- loop over the result of a query<A NAME="IX-PGTCL-PGSELECT-2" ></A ></DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN22619" ></A ><H2 >Synopsis</H2 ><PRE CLASS="SYNOPSIS" >pg_select <VAR CLASS="PARAMETER" >conn</VAR > <VAR CLASS="PARAMETER" >commandString</VAR > <VAR CLASS="PARAMETER" >arrayVar</VAR > <VAR CLASS="PARAMETER" >procedure</VAR ></PRE ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN22625" ></A ><H2 >Description</H2 ><P > <CODE CLASS="FUNCTION" >pg_select</CODE > submits a query (<TT CLASS="COMMAND" >SELECT</TT > statement) to the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > server and executes a given chunk of code for each row in the result. The <VAR CLASS="PARAMETER" >commandString</VAR > must be a <TT CLASS="COMMAND" >SELECT</TT > statement; anything else returns an error. The <VAR CLASS="PARAMETER" >arrayVar</VAR > variable is an array name used in the loop. For each row, <VAR CLASS="PARAMETER" >arrayVar</VAR > is filled in with the row values, using the column names as the array indices. Then the <VAR CLASS="PARAMETER" >procedure</VAR > is executed. </P ><P > In addition to the column values, the following special entries are made in the array: <P ></P ></P><DIV CLASS="VARIABLELIST" ><DL ><DT ><TT CLASS="LITERAL" >.headers</TT ></DT ><DD ><P > A list of the column names returned by the query. </P ></DD ><DT ><TT CLASS="LITERAL" >.numcols</TT ></DT ><DD ><P > The number of columns returned by the query. </P ></DD ><DT ><TT CLASS="LITERAL" >.tupno</TT ></DT ><DD ><P > The current row number, starting at zero and incrementing for each iteration of the loop body. </P ></DD ></DL ></DIV ><P> </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN22653" ></A ><H2 >Arguments</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><VAR CLASS="PARAMETER" >conn</VAR ></DT ><DD ><P > The handle of the connection on which to execute the query. </P ></DD ><DT ><VAR CLASS="PARAMETER" >commandString</VAR ></DT ><DD ><P > The SQL query to execute. </P ></DD ><DT ><VAR CLASS="PARAMETER" >arrayVar</VAR ></DT ><DD ><P > An array variable for returned rows. </P ></DD ><DT ><VAR CLASS="PARAMETER" >procedure</VAR ></DT ><DD ><P > The procedure to run for each returned row. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN22676" ></A ><H2 >Return Value</H2 ><P > None </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN22679" ></A ><H2 >Examples</H2 ><P > This examples assumes that the table <CODE CLASS="CLASSNAME" >table1</CODE > has columns <TT CLASS="STRUCTFIELD" >control</TT > and <TT CLASS="STRUCTFIELD" >name</TT > (and perhaps others): </P><PRE CLASS="PROGRAMLISTING" >pg_select $pgconn "SELECT * FROM table1;" array { puts [format "%5d %s" $array(control) $array(name)] }</PRE ><P> </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="pgtcl-pgresult.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="pgtcl-pgexecute.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_result</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="libpgtcl-ref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >pg_execute</TD ></TR ></TABLE ></DIV ></BODY ></HTML >