Home Explore Blog CI



postgresql

85th chunk of `doc/src/sgml/ecpg.sgml`
fd83584255ada6d8db0eb4e2edd01d7143bf3c813f4881220000000100000fa7
 can also
     be used in embedded programs.  The <xref linkend="sql-execute"/>
     command is used to execute either kind of prepared statement.
    </para>
   </refsect1>

   <refsect1>
    <title>Parameters</title>

    <variablelist>
     <varlistentry id="ecpg-sql-prepare-prepared-name">
      <term><replaceable class="parameter">prepared_name</replaceable></term>
      <listitem>
       <para>
        An identifier for the prepared query.
       </para>
      </listitem>
     </varlistentry>

     <varlistentry id="ecpg-sql-prepare-string">
      <term><replaceable class="parameter">string</replaceable></term>
      <listitem>
       <para>
        A literal string or a host variable containing a preparable
        SQL statement, one of SELECT, INSERT, UPDATE, or DELETE.
        Use question marks (<literal>?</literal>) for parameter values
        to be supplied at execution.
       </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 a direct SQL <command>PREPARE</command>
     statement.
    </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>
<programlisting>
char *stmt = "SELECT * FROM test1 WHERE a = ? AND b = ?";

EXEC SQL ALLOCATE DESCRIPTOR outdesc;
EXEC SQL PREPARE foo FROM :stmt;

EXEC SQL EXECUTE foo USING SQL DESCRIPTOR indesc INTO SQL DESCRIPTOR outdesc;
</programlisting>
   </refsect1>

   <refsect1>
    <title>Compatibility</title>

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

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

    <simplelist type="inline">
     <member><xref linkend="sql-execute"/></member>
    </simplelist>
   </refsect1>
  </refentry>

  <refentry id="ecpg-sql-set-autocommit">
   <refnamediv>
    <refname>SET AUTOCOMMIT</refname>
    <refpurpose>set the autocommit behavior of the current session</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
SET AUTOCOMMIT { = | TO } { ON | OFF }
</synopsis>
   </refsynopsisdiv>

   <refsect1>
    <title>Description</title>

    <para>
     <command>SET AUTOCOMMIT</command> sets the autocommit behavior of
     the current database session.  By default, embedded SQL programs
     are <emphasis>not</emphasis> in autocommit mode,
     so <command>COMMIT</command> needs to be issued explicitly when
     desired.  This command can change the session to autocommit mode,
     where each individual statement is committed implicitly.
    </para>
   </refsect1>

   <refsect1>
    <title>Compatibility</title>

    <para>
     <command>SET AUTOCOMMIT</command> is an extension of PostgreSQL ECPG.
    </para>
   </refsect1>
  </refentry>

  <refentry id="ecpg-sql-set-connection">
   <refnamediv>
    <refname>SET CONNECTION</refname>
    <refpurpose>select a database connection</refpurpose>
   </refnamediv>

   <refsynopsisdiv>
<synopsis>
SET CONNECTION [ TO | = ] <replaceable class="parameter">connection_name</replaceable>
</synopsis>
   </refsynopsisdiv>

   <refsect1>
    <title>Description</title>

    <para>
     <command>SET CONNECTION</command> sets the <quote>current</quote>

Title: ECPG PREPARE: Usage Notes, Examples, and SET AUTOCOMMIT/CONNECTION
Summary
This section elaborates on the ECPG `PREPARE` command, advising on using host variables for dynamic SQL statement construction. It clarifies how to handle double quotes in literal strings within `EXEC SQL` sections. An example illustrates preparing and executing a statement using descriptors. The command's SQL standard compatibility is noted. Then, it introduces the `SET AUTOCOMMIT` command, which configures the autocommit behavior of a database session in ECPG, indicating it's a PostgreSQL extension and that embedded SQL programs are not in autocommit mode by default. Finally, the `SET CONNECTION` command is introduced, which selects a database connection.