<!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 REL="HOME" TITLE="Pgtcl Reference Manual" HREF="index.html"><LINK REL="UP" TITLE="Query Execution Commands" HREF="pgtcl-ref-query.html"><LINK REL="PREVIOUS" TITLE="pg_execute" HREF="pg-execute.html"><LINK REL="NEXT" TITLE="pg_exec_params" HREF="pg-exec-params.html"><LINK REL="STYLESHEET" TYPE="text/css" HREF="stylesheet.css"><META NAME="creation" CONTENT="2004-11-09T00:53:06"></HEAD ><BODY CLASS="REFENTRY" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >Pgtcl Reference Manual: The PostgreSQL Tcl Interface</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="pg-execute.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="pg-exec-params.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="PG-SELECT" ></A >pg_select</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN1048" ></A ><H2 >Name</H2 >pg_select -- loop over the result of a query</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN1051" ></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="AEN1057" ></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="REPLACEABLE" >commandString</VAR > must be a <TT CLASS="COMMAND" >SELECT</TT > statement; anything else returns an error. The <VAR CLASS="REPLACEABLE" >arrayVar</VAR > variable is an array name used in the loop. For each row, <VAR CLASS="REPLACEABLE" >arrayVar</VAR > is filled in with the row values, using the column names as the array indices. Then the <VAR CLASS="REPLACEABLE" >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="AEN1085" ></A ><H2 >Arguments</H2 ><P ></P ><DIV CLASS="VARIABLELIST" ><DL ><DT ><VAR CLASS="REPLACEABLE" >conn</VAR ></DT ><DD ><P > The handle of the connection on which to execute the query. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >commandString</VAR ></DT ><DD ><P > The SQL query to execute. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >arrayVar</VAR ></DT ><DD ><P > An array variable for returned rows. </P ></DD ><DT ><VAR CLASS="REPLACEABLE" >procedure</VAR ></DT ><DD ><P > The procedure or script to run for each returned row. </P ></DD ></DL ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN1108" ></A ><H2 >Return Value</H2 ><P > None </P ><P > A Tcl error will be thrown if an error occurs communicating with the database, or if there is an error processing the query, or if the procedure body throws a Tcl error. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN1112" ></A ><H2 >Notes</H2 ><P > <A HREF="pg-execute.html" >pg_execute</A > is a newer command which provides more features and is more flexible than <CODE CLASS="FUNCTION" >pg_select</CODE >. </P ><P > This command uses or emulates the <SPAN CLASS="PRODUCTNAME" >PostgreSQL</SPAN > <SPAN CLASS="APPLICATION" >libpq</SPAN > function <CODE CLASS="FUNCTION" >PQexec</CODE >. </P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN1121" ></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 $conn "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="pg-execute.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="pg-exec-params.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >pg_execute</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="pgtcl-ref-query.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >pg_exec_params</TD ></TR ></TABLE ></DIV ></BODY ></HTML >