]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-dynamic.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-dynamic.txt
1
2 34.5. Dynamic SQL #
3
4    34.5.1. Executing Statements without a Result Set
5    34.5.2. Executing a Statement with Input Parameters
6    34.5.3. Executing a Statement with a Result Set
7
8    In many cases, the particular SQL statements that an application has to
9    execute are known at the time the application is written. In some
10    cases, however, the SQL statements are composed at run time or provided
11    by an external source. In these cases you cannot embed the SQL
12    statements directly into the C source code, but there is a facility
13    that allows you to call arbitrary SQL statements that you provide in a
14    string variable.
15
16 34.5.1. Executing Statements without a Result Set #
17
18    The simplest way to execute an arbitrary SQL statement is to use the
19    command EXECUTE IMMEDIATE. For example:
20 EXEC SQL BEGIN DECLARE SECTION;
21 const char *stmt = "CREATE TABLE test1 (...);";
22 EXEC SQL END DECLARE SECTION;
23
24 EXEC SQL EXECUTE IMMEDIATE :stmt;
25
26    EXECUTE IMMEDIATE can be used for SQL statements that do not return a
27    result set (e.g., DDL, INSERT, UPDATE, DELETE). You cannot execute
28    statements that retrieve data (e.g., SELECT) this way. The next section
29    describes how to do that.
30
31 34.5.2. Executing a Statement with Input Parameters #
32
33    A more powerful way to execute arbitrary SQL statements is to prepare
34    them once and execute the prepared statement as often as you like. It
35    is also possible to prepare a generalized version of a statement and
36    then execute specific versions of it by substituting parameters. When
37    preparing the statement, write question marks where you want to
38    substitute parameters later. For example:
39 EXEC SQL BEGIN DECLARE SECTION;
40 const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
41 EXEC SQL END DECLARE SECTION;
42
43 EXEC SQL PREPARE mystmt FROM :stmt;
44  ...
45 EXEC SQL EXECUTE mystmt USING 42, 'foobar';
46
47    When you don't need the prepared statement anymore, you should
48    deallocate it:
49 EXEC SQL DEALLOCATE PREPARE name;
50
51 34.5.3. Executing a Statement with a Result Set #
52
53    To execute an SQL statement with a single result row, EXECUTE can be
54    used. To save the result, add an INTO clause.
55 EXEC SQL BEGIN DECLARE SECTION;
56 const char *stmt = "SELECT a, b, c FROM test1 WHERE a > ?";
57 int v1, v2;
58 VARCHAR v3[50];
59 EXEC SQL END DECLARE SECTION;
60
61 EXEC SQL PREPARE mystmt FROM :stmt;
62  ...
63 EXEC SQL EXECUTE mystmt INTO :v1, :v2, :v3 USING 37;
64
65
66    An EXECUTE command can have an INTO clause, a USING clause, both, or
67    neither.
68
69    If a query is expected to return more than one result row, a cursor
70    should be used, as in the following example. (See Section 34.3.2 for
71    more details about the cursor.)
72 EXEC SQL BEGIN DECLARE SECTION;
73 char dbaname[128];
74 char datname[128];
75 char *stmt = "SELECT u.usename as dbaname, d.datname "
76              "  FROM pg_database d, pg_user u "
77              "  WHERE d.datdba = u.usesysid";
78 EXEC SQL END DECLARE SECTION;
79
80 EXEC SQL CONNECT TO testdb AS con1 USER testuser;
81 EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT
82 ;
83
84 EXEC SQL PREPARE stmt1 FROM :stmt;
85
86 EXEC SQL DECLARE cursor1 CURSOR FOR stmt1;
87 EXEC SQL OPEN cursor1;
88
89 EXEC SQL WHENEVER NOT FOUND DO BREAK;
90
91 while (1)
92 {
93     EXEC SQL FETCH cursor1 INTO :dbaname,:datname;
94     printf("dbaname=%s, datname=%s\n", dbaname, datname);
95 }
96
97 EXEC SQL CLOSE cursor1;
98
99 EXEC SQL COMMIT;
100 EXEC SQL DISCONNECT ALL;