Home Explore Blog CI



postgresql

77th chunk of `doc/src/sgml/ecpg.sgml`
2365104ec6d66d5336b397af680983f3f648f7856000cc2c0000000100000fa2
 <simplelist type="inline">
     <member><xref linkend="ecpg-sql-allocate-descriptor"/></member>
     <member><xref linkend="ecpg-sql-get-descriptor"/></member>
     <member><xref linkend="ecpg-sql-set-descriptor"/></member>
    </simplelist>
   </refsect1>
  </refentry>

  <refentry id="ecpg-sql-declare">
   <refnamediv>
    <refname>DECLARE</refname>
    <refpurpose>define a cursor</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
DECLARE <replaceable class="parameter">cursor_name</replaceable> [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR <replaceable class="parameter">prepared_name</replaceable>
DECLARE <replaceable class="parameter">cursor_name</replaceable> [ BINARY ] [ ASENSITIVE | INSENSITIVE ] [ [ NO ] SCROLL ] CURSOR [ { WITH | WITHOUT } HOLD ] FOR <replaceable class="parameter">query</replaceable>
</synopsis>
   </refsynopsisdiv>

   <refsect1>
    <title>Description</title>

    <para>
     <command>DECLARE</command> declares a cursor for iterating over
     the result set of a prepared statement.  This command has
     slightly different semantics from the direct SQL
     command <command>DECLARE</command>: Whereas the latter executes a
     query and prepares the result set for retrieval, this embedded
     SQL command merely declares a name as a <quote>loop
     variable</quote> for iterating over the result set of a query;
     the actual execution happens when the cursor is opened with
     the <command>OPEN</command> command.
    </para>
   </refsect1>

   <refsect1>
    <title>Parameters</title>
    <variablelist>

     <varlistentry id="ecpg-sql-declare-cursor-name">
      <term><replaceable class="parameter">cursor_name</replaceable></term>
      <listitem>
       <para>
        A cursor name, case sensitive.  This can be an SQL identifier
        or a host variable.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-sql-declare-prepared-name">
      <term><replaceable class="parameter">prepared_name</replaceable></term>
      <listitem>
       <para>
        The name of a prepared query, either as an SQL identifier or a
        host variable.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-sql-declare-query">
      <term><replaceable class="parameter">query</replaceable></term>
      <listitem>
       <para>
        A <xref linkend="sql-select"/> or
        <xref linkend="sql-values"/> command which will provide the
        rows to be returned by the cursor.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>

    <para>
     For the meaning of the cursor options,
     see <xref linkend="sql-declare"/>.
    </para>
   </refsect1>

   <refsect1>
    <title>Examples</title>

    <para>
     Examples declaring a cursor for a query:
<programlisting>
EXEC SQL DECLARE C CURSOR FOR SELECT * FROM My_Table;
EXEC SQL DECLARE C CURSOR FOR SELECT Item1 FROM T;
EXEC SQL DECLARE cur1 CURSOR FOR SELECT version();
</programlisting>
    </para>

    <para>
     An example declaring a cursor for a prepared statement:
<programlisting>
EXEC SQL PREPARE stmt1 AS SELECT version();
EXEC SQL DECLARE cur1 CURSOR FOR stmt1;
</programlisting>
    </para>
   </refsect1>

   <refsect1>
    <title>Compatibility</title>

    <para>
     <command>DECLARE</command> is specified in the SQL standard.
    </para>
   </refsect1>

   <refsect1>
    <title>See Also</title>

    <simplelist type="inline">
     <member><xref linkend="ecpg-sql-open"/></member>
     <member><xref linkend="sql-close"/></member>
     <member><xref linkend="sql-declare"/></member>
    </simplelist>
   </refsect1>
  </refentry>

  <refentry id="ecpg-sql-declare-statement">
   <refnamediv>
    <refname>DECLARE STATEMENT</refname>
    <refpurpose>declare SQL statement identifier</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
EXEC SQL [ AT <replaceable class="parameter">connection_name</replaceable> ] DECLARE

Title: ECPG DECLARE Command for Cursor Definition
Summary
This section describes the ECPG `DECLARE` command, which defines a cursor for iterating over the result set of a prepared statement or query. It explains the syntax, parameters like `cursor_name`, `prepared_name`, and `query`, and their usage. The command differs from the direct SQL `DECLARE` command in that it declares a name for iterating over the result set, while the actual execution happens with the `OPEN` command. Examples show declaring cursors for both queries and prepared statements. Compatibility with the SQL standard and related commands like `OPEN`, `CLOSE`, and the SQL `DECLARE` command are also mentioned.