Home Explore Blog CI



postgresql

81th chunk of `doc/src/sgml/ecpg.sgml`
8b8b5dbc9e99939ede3448e89c68d791977c82fce89a6ad50000000100000fa3
 <refpurpose>dynamically prepare and execute a statement</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
EXECUTE IMMEDIATE <replaceable class="parameter">string</replaceable>
</synopsis>
   </refsynopsisdiv>

   <refsect1>
    <title>Description</title>

    <para>
     <command>EXECUTE IMMEDIATE</command> immediately prepares and
     executes a dynamically specified SQL statement, without
     retrieving result rows.
    </para>
   </refsect1>

   <refsect1>
    <title>Parameters</title>

    <variablelist>
     <varlistentry id="ecpg-sql-execute-immediate-string">
      <term><replaceable class="parameter">string</replaceable></term>
      <listitem>
       <para>
        A literal string or a host variable containing the SQL
        statement to be executed.
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
   </refsect1>

   <refsect1>
    <title>Notes</title>

    <para>
     In typical usage, the <replaceable>string</replaceable> is a host
     variable reference to a string containing a dynamically-constructed
     SQL statement.  The case of a literal string is not very useful;
     you might as well just write the SQL statement directly, without
     the extra typing of <command>EXECUTE IMMEDIATE</command>.
    </para>

    <para>
     If you do use a literal string, keep in mind that any double quotes
     you might wish to include in the SQL statement must be written as
     octal escapes (<literal>\042</literal>) not the usual C
     idiom <literal>\"</literal>.  This is because the string is inside
     an <literal>EXEC SQL</literal> section, so the ECPG lexer parses it
     according to SQL rules not C rules.  Any embedded backslashes will
     later be handled according to C rules; but <literal>\"</literal>
     causes an immediate syntax error because it is seen as ending the
     literal.
    </para>
   </refsect1>

   <refsect1>
    <title>Examples</title>

    <para>
     Here is an example that executes an <command>INSERT</command>
     statement using <command>EXECUTE IMMEDIATE</command> and a host
     variable named <varname>command</varname>:
<programlisting>
sprintf(command, "INSERT INTO test (name, amount, letter) VALUES ('db: ''r1''', 1, 'f')");
EXEC SQL EXECUTE IMMEDIATE :command;
</programlisting>
    </para>
   </refsect1>

   <refsect1>
    <title>Compatibility</title>

    <para>
     <command>EXECUTE IMMEDIATE</command> is specified in the SQL standard.
    </para>
   </refsect1>
  </refentry>

  <refentry id="ecpg-sql-get-descriptor">
   <refnamediv>
    <refname>GET DESCRIPTOR</refname>
    <refpurpose>get information from an SQL descriptor area</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
GET DESCRIPTOR <replaceable class="parameter">descriptor_name</replaceable> <replaceable class="parameter">:cvariable</replaceable> = <replaceable class="parameter">descriptor_header_item</replaceable> [, ... ]
GET DESCRIPTOR <replaceable class="parameter">descriptor_name</replaceable> VALUE <replaceable class="parameter">column_number</replaceable> <replaceable class="parameter">:cvariable</replaceable> = <replaceable class="parameter">descriptor_item</replaceable> [, ... ]
</synopsis>
   </refsynopsisdiv>

   <refsect1>
    <title>Description</title>

    <para>
     <command>GET DESCRIPTOR</command> retrieves information about a
     query result set from an SQL descriptor area and stores it into
     host variables.  A descriptor area is typically populated
     using <command>FETCH</command> or <command>SELECT</command>
     before using this command to transfer the information into host
     language variables.
    </para>

    <para>
     This command has two forms: The first form retrieves
     descriptor <quote>header</quote> items, which apply to the result
     set in its entirety.  One example is the row count.  The second
     form, which requires the column number as additional parameter,
     retrieves information about a particular column.

Title: ECPG EXECUTE IMMEDIATE Command: Dynamic SQL Execution
Summary
This section details the `EXECUTE IMMEDIATE` command in ECPG, which prepares and executes dynamically constructed SQL statements. It emphasizes the use of host variables for the SQL string and notes that using literal strings is generally less useful. The section also clarifies the handling of double quotes within literal strings inside `EXEC SQL` sections, requiring octal escapes instead of the usual C idiom. An example demonstrates executing an `INSERT` statement using a host variable. The section concludes by stating that `EXECUTE IMMEDIATE` is part of the SQL standard.