]> begriffs open source - ai-pg/blob - full-docs/txt/ecpg-sql-whenever.txt
Convert HTML docs to more streamlined TXT
[ai-pg] / full-docs / txt / ecpg-sql-whenever.txt
1
2 WHENEVER
3
4    WHENEVER — specify the action to be taken when an SQL statement causes
5    a specific class condition to be raised
6
7 Synopsis
8
9 WHENEVER { NOT FOUND | SQLERROR | SQLWARNING } action
10
11 Description
12
13    Define a behavior which is called on the special cases (Rows not found,
14    SQL warnings or errors) in the result of SQL execution.
15
16 Parameters
17
18    See Section 34.8.1 for a description of the parameters.
19
20 Examples
21
22 EXEC SQL WHENEVER NOT FOUND CONTINUE;
23 EXEC SQL WHENEVER NOT FOUND DO BREAK;
24 EXEC SQL WHENEVER NOT FOUND DO CONTINUE;
25 EXEC SQL WHENEVER SQLWARNING SQLPRINT;
26 EXEC SQL WHENEVER SQLWARNING DO warn();
27 EXEC SQL WHENEVER SQLERROR sqlprint;
28 EXEC SQL WHENEVER SQLERROR CALL print2();
29 EXEC SQL WHENEVER SQLERROR DO handle_error("select");
30 EXEC SQL WHENEVER SQLERROR DO sqlnotice(NULL, NONO);
31 EXEC SQL WHENEVER SQLERROR DO sqlprint();
32 EXEC SQL WHENEVER SQLERROR GOTO error_label;
33 EXEC SQL WHENEVER SQLERROR STOP;
34
35    A typical application is the use of WHENEVER NOT FOUND BREAK to handle
36    looping through result sets:
37 int
38 main(void)
39 {
40     EXEC SQL CONNECT TO testdb AS con1;
41     EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL CO
42 MMIT;
43     EXEC SQL ALLOCATE DESCRIPTOR d;
44     EXEC SQL DECLARE cur CURSOR FOR SELECT current_database(), 'hoge', 256;
45     EXEC SQL OPEN cur;
46
47     /* when end of result set reached, break out of while loop */
48     EXEC SQL WHENEVER NOT FOUND DO BREAK;
49
50     while (1)
51     {
52         EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d;
53         ...
54     }
55
56     EXEC SQL CLOSE cur;
57     EXEC SQL COMMIT;
58
59     EXEC SQL DEALLOCATE DESCRIPTOR d;
60     EXEC SQL DISCONNECT ALL;
61
62     return 0;
63 }
64
65 Compatibility
66
67    WHENEVER is specified in the SQL standard, but most of the actions are
68    PostgreSQL extensions.