2 34.3. Running SQL Commands #
4 34.3.1. Executing SQL Statements
6 34.3.3. Managing Transactions
7 34.3.4. Prepared Statements
9 Any SQL command can be run from within an embedded SQL application.
10 Below are some examples of how to do that.
12 34.3.1. Executing SQL Statements #
15 EXEC SQL CREATE TABLE foo (number integer, ascii char(16));
16 EXEC SQL CREATE UNIQUE INDEX num1 ON foo(number);
20 EXEC SQL INSERT INTO foo (number, ascii) VALUES (9999, 'doodad');
24 EXEC SQL DELETE FROM foo WHERE number = 9999;
33 SELECT statements that return a single result row can also be executed
34 using EXEC SQL directly. To handle result sets with multiple rows, an
35 application has to use a cursor; see Section 34.3.2 below. (As a
36 special case, an application can fetch multiple rows at once into an
37 array host variable; see Section 34.4.4.3.1.)
40 EXEC SQL SELECT foo INTO :FooBar FROM table1 WHERE ascii = 'doodad';
42 Also, a configuration parameter can be retrieved with the SHOW command:
43 EXEC SQL SHOW search_path INTO :var;
45 The tokens of the form :something are host variables, that is, they
46 refer to variables in the C program. They are explained in
49 34.3.2. Using Cursors #
51 To retrieve a result set holding multiple rows, an application has to
52 declare a cursor and fetch each row from the cursor. The steps to use a
53 cursor are the following: declare a cursor, open it, fetch a row from
54 the cursor, repeat, and finally close it.
57 EXEC SQL DECLARE foo_bar CURSOR FOR
58 SELECT number, ascii FROM foo
60 EXEC SQL OPEN foo_bar;
61 EXEC SQL FETCH foo_bar INTO :FooBar, DooDad;
63 EXEC SQL CLOSE foo_bar;
66 For more details about declaring a cursor, see DECLARE; for more
67 details about fetching rows from a cursor, see FETCH.
71 The ECPG DECLARE command does not actually cause a statement to be sent
72 to the PostgreSQL backend. The cursor is opened in the backend (using
73 the backend's DECLARE command) at the point when the OPEN command is
76 34.3.3. Managing Transactions #
78 In the default mode, statements are committed only when EXEC SQL COMMIT
79 is issued. The embedded SQL interface also supports autocommit of
80 transactions (similar to psql's default behavior) via the -t
81 command-line option to ecpg (see ecpg) or via the EXEC SQL SET
82 AUTOCOMMIT TO ON statement. In autocommit mode, each command is
83 automatically committed unless it is inside an explicit transaction
84 block. This mode can be explicitly turned off using EXEC SQL SET
87 The following transaction management commands are available:
90 Commit an in-progress transaction.
93 Roll back an in-progress transaction.
95 EXEC SQL PREPARE TRANSACTION transaction_id #
96 Prepare the current transaction for two-phase commit.
98 EXEC SQL COMMIT PREPARED transaction_id #
99 Commit a transaction that is in prepared state.
101 EXEC SQL ROLLBACK PREPARED transaction_id #
102 Roll back a transaction that is in prepared state.
104 EXEC SQL SET AUTOCOMMIT TO ON #
105 Enable autocommit mode.
107 EXEC SQL SET AUTOCOMMIT TO OFF #
108 Disable autocommit mode. This is the default.
110 34.3.4. Prepared Statements #
112 When the values to be passed to an SQL statement are not known at
113 compile time, or the same statement is going to be used many times,
114 then prepared statements can be useful.
116 The statement is prepared using the command PREPARE. For the values
117 that are not known yet, use the placeholder “?”:
118 EXEC SQL PREPARE stmt1 FROM "SELECT oid, datname FROM pg_database WHERE oid = ?"
121 If a statement returns a single row, the application can call EXECUTE
122 after PREPARE to execute the statement, supplying the actual values for
123 the placeholders with a USING clause:
124 EXEC SQL EXECUTE stmt1 INTO :dboid, :dbname USING 1;
126 If a statement returns multiple rows, the application can use a cursor
127 declared based on the prepared statement. To bind input parameters, the
128 cursor must be opened with a USING clause:
129 EXEC SQL PREPARE stmt1 FROM "SELECT oid,datname FROM pg_database WHERE oid > ?";
130 EXEC SQL DECLARE foo_bar CURSOR FOR stmt1;
132 /* when end of result set reached, break out of while loop */
133 EXEC SQL WHENEVER NOT FOUND DO BREAK;
135 EXEC SQL OPEN foo_bar USING 100;
139 EXEC SQL FETCH NEXT FROM foo_bar INTO :dboid, :dbname;
142 EXEC SQL CLOSE foo_bar;
144 When you don't need the prepared statement anymore, you should
146 EXEC SQL DEALLOCATE PREPARE name;
148 For more details about PREPARE, see PREPARE. Also see Section 34.5 for
149 more details about using placeholders and input parameters.