diff options
Diffstat (limited to 'doc/src/sgml/html/ecpg-commands.html')
-rw-r--r-- | doc/src/sgml/html/ecpg-commands.html | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/doc/src/sgml/html/ecpg-commands.html b/doc/src/sgml/html/ecpg-commands.html new file mode 100644 index 0000000..e171783 --- /dev/null +++ b/doc/src/sgml/html/ecpg-commands.html @@ -0,0 +1,165 @@ +<?xml version="1.0" encoding="UTF-8" standalone="no"?> +<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>35.3. Running SQL Commands</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="ecpg-connect.html" title="35.2. Managing Database Connections" /><link rel="next" href="ecpg-variables.html" title="35.4. Using Host Variables" /></head><body id="docContent" class="container-fluid col-10"><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">35.3. Running SQL Commands</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-connect.html" title="35.2. Managing Database Connections">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 35. ECPG — Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 35. <span xmlns="http://www.w3.org/1999/xhtml" class="application">ECPG</span> — Embedded <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> in C</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-variables.html" title="35.4. Using Host Variables">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-COMMANDS"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.3. Running SQL Commands</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-commands.html#ECPG-EXECUTING">35.3.1. Executing SQL Statements</a></span></dt><dt><span class="sect2"><a href="ecpg-commands.html#ECPG-CURSORS">35.3.2. Using Cursors</a></span></dt><dt><span class="sect2"><a href="ecpg-commands.html#ECPG-TRANSACTIONS">35.3.3. Managing Transactions</a></span></dt><dt><span class="sect2"><a href="ecpg-commands.html#ECPG-PREPARED">35.3.4. Prepared Statements</a></span></dt></dl></div><p> + Any SQL command can be run from within an embedded SQL application. + Below are some examples of how to do that. + </p><div class="sect2" id="ECPG-EXECUTING"><div class="titlepage"><div><div><h3 class="title">35.3.1. Executing SQL Statements</h3></div></div></div><p> + Creating a table: +</p><pre class="programlisting"> +EXEC SQL CREATE TABLE foo (number integer, ascii char(16)); +EXEC SQL CREATE UNIQUE INDEX num1 ON foo(number); +EXEC SQL COMMIT; +</pre><p> + </p><p> + Inserting rows: +</p><pre class="programlisting"> +EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad'); +EXEC SQL COMMIT; +</pre><p> + </p><p> + Deleting rows: +</p><pre class="programlisting"> +EXEC SQL DELETE FROM foo WHERE number = 9999; +EXEC SQL COMMIT; +</pre><p> + </p><p> + Updates: +</p><pre class="programlisting"> +EXEC SQL UPDATE foo + SET ascii = 'foobar' + WHERE number = 9999; +EXEC SQL COMMIT; +</pre><p> + </p><p> + <code class="literal">SELECT</code> statements that return a single result + row can also be executed using + <code class="literal">EXEC SQL</code> directly. To handle result sets with + multiple rows, an application has to use a cursor; + see <a class="xref" href="ecpg-commands.html#ECPG-CURSORS" title="35.3.2. Using Cursors">Section 35.3.2</a> below. (As a special case, an + application can fetch multiple rows at once into an array host + variable; see <a class="xref" href="ecpg-variables.html#ECPG-VARIABLES-ARRAYS" title="35.4.4.3.1. Arrays">Section 35.4.4.3.1</a>.) + </p><p> + Single-row select: +</p><pre class="programlisting"> +EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = 'doodad'; +</pre><p> + </p><p> + Also, a configuration parameter can be retrieved with the + <code class="literal">SHOW</code> command: +</p><pre class="programlisting"> +EXEC SQL SHOW search_path INTO :var; +</pre><p> + </p><p> + The tokens of the form + <code class="literal">:<em class="replaceable"><code>something</code></em></code> are + <em class="firstterm">host variables</em>, that is, they refer to + variables in the C program. They are explained in <a class="xref" href="ecpg-variables.html" title="35.4. Using Host Variables">Section 35.4</a>. + </p></div><div class="sect2" id="ECPG-CURSORS"><div class="titlepage"><div><div><h3 class="title">35.3.2. Using Cursors</h3></div></div></div><p> + To retrieve a result set holding multiple rows, an application has + to declare a cursor and fetch each row from the cursor. The steps + to use a cursor are the following: declare a cursor, open it, fetch + a row from the cursor, repeat, and finally close it. + </p><p> + Select using cursors: +</p><pre class="programlisting"> +EXEC SQL DECLARE foo_bar CURSOR FOR + SELECT number, ascii FROM foo + ORDER BY ascii; +EXEC SQL OPEN foo_bar; +EXEC SQL FETCH foo_bar INTO :FooBar, DooDad; +... +EXEC SQL CLOSE foo_bar; +EXEC SQL COMMIT; +</pre><p> + </p><p> + For more details about declaration of the cursor, + see <a class="xref" href="ecpg-sql-declare.html" title="DECLARE">DECLARE</a>, and + see <a class="xref" href="sql-fetch.html" title="FETCH"><span class="refentrytitle">FETCH</span></a> for <code class="literal">FETCH</code> command + details. + </p><div class="note"><h3 class="title">Note</h3><p> + The ECPG <code class="command">DECLARE</code> command does not actually + cause a statement to be sent to the PostgreSQL backend. The + cursor is opened in the backend (using the + backend's <code class="command">DECLARE</code> command) at the point when + the <code class="command">OPEN</code> command is executed. + </p></div></div><div class="sect2" id="ECPG-TRANSACTIONS"><div class="titlepage"><div><div><h3 class="title">35.3.3. Managing Transactions</h3></div></div></div><p> + In the default mode, statements are committed only when + <code class="command">EXEC SQL COMMIT</code> is issued. The embedded SQL + interface also supports autocommit of transactions (similar to + <span class="application">psql</span>'s default behavior) via the <code class="option">-t</code> + command-line option to <code class="command">ecpg</code> (see <a class="xref" href="app-ecpg.html" title="ecpg"><span class="refentrytitle"><span class="application">ecpg</span></span></a>) or via the <code class="literal">EXEC SQL SET AUTOCOMMIT TO + ON</code> statement. In autocommit mode, each command is + automatically committed unless it is inside an explicit transaction + block. This mode can be explicitly turned off using <code class="literal">EXEC + SQL SET AUTOCOMMIT TO OFF</code>. + </p><p> + The following transaction management commands are available: + + </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">EXEC SQL COMMIT</code></span></dt><dd><p> + Commit an in-progress transaction. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL ROLLBACK</code></span></dt><dd><p> + Roll back an in-progress transaction. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL PREPARE TRANSACTION </code><em class="replaceable"><code>transaction_id</code></em></span></dt><dd><p> + Prepare the current transaction for two-phase commit. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL COMMIT PREPARED </code><em class="replaceable"><code>transaction_id</code></em></span></dt><dd><p> + Commit a transaction that is in prepared state. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL ROLLBACK PREPARED </code><em class="replaceable"><code>transaction_id</code></em></span></dt><dd><p> + Roll back a transaction that is in prepared state. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL SET AUTOCOMMIT TO ON</code></span></dt><dd><p> + Enable autocommit mode. + </p></dd><dt><span class="term"><code class="literal">EXEC SQL SET AUTOCOMMIT TO OFF</code></span></dt><dd><p> + Disable autocommit mode. This is the default. + </p></dd></dl></div><p> + </p></div><div class="sect2" id="ECPG-PREPARED"><div class="titlepage"><div><div><h3 class="title">35.3.4. Prepared Statements</h3></div></div></div><p> + When the values to be passed to an SQL statement are not known at + compile time, or the same statement is going to be used many + times, then prepared statements can be useful. + </p><p> + The statement is prepared using the + command <code class="literal">PREPARE</code>. For the values that are not + known yet, use the + placeholder <span class="quote">“<span class="quote"><code class="literal">?</code></span>”</span>: +</p><pre class="programlisting"> +EXEC SQL PREPARE stmt1 FROM "SELECT oid, datname FROM pg_database WHERE oid = ?"; +</pre><p> + </p><p> + If a statement returns a single row, the application can + call <code class="literal">EXECUTE</code> after + <code class="literal">PREPARE</code> to execute the statement, supplying the + actual values for the placeholders with a <code class="literal">USING</code> + clause: +</p><pre class="programlisting"> +EXEC SQL EXECUTE stmt1 INTO :dboid, :dbname USING 1; +</pre><p> + </p><p> + If a statement returns multiple rows, the application can use a + cursor declared based on the prepared statement. To bind input + parameters, the cursor must be opened with + a <code class="literal">USING</code> clause: +</p><pre class="programlisting"> +EXEC SQL PREPARE stmt1 FROM "SELECT oid,datname FROM pg_database WHERE oid > ?"; +EXEC SQL DECLARE foo_bar CURSOR FOR stmt1; + +/* when end of result set reached, break out of while loop */ +EXEC SQL WHENEVER NOT FOUND DO BREAK; + +EXEC SQL OPEN foo_bar USING 100; +... +while (1) +{ + EXEC SQL FETCH NEXT FROM foo_bar INTO :dboid, :dbname; + ... +} +EXEC SQL CLOSE foo_bar; +</pre><p> + </p><p> + When you don't need the prepared statement anymore, you should + deallocate it: +</p><pre class="programlisting"> +EXEC SQL DEALLOCATE PREPARE <em class="replaceable"><code>name</code></em>; +</pre><p> + </p><p> + For more details about <code class="literal">PREPARE</code>, + see <a class="xref" href="ecpg-sql-prepare.html" title="PREPARE">PREPARE</a>. Also + see <a class="xref" href="ecpg-dynamic.html" title="35.5. Dynamic SQL">Section 35.5</a> for more details about using + placeholders and input parameters. + </p></div></div><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navfooter"><hr></hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-connect.html" title="35.2. Managing Database Connections">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html" title="Chapter 35. ECPG — Embedded SQL in C">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-variables.html" title="35.4. Using Host Variables">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.2. Managing Database Connections </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 13.4 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 35.4. Using Host Variables</td></tr></table></div></body></html>
\ No newline at end of file |