1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!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>WHENEVER</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 Vsnapshot" /><link rel="prev" href="ecpg-sql-var.html" title="VAR" /><link rel="next" href="ecpg-informix-compat.html" title="34.15. Informix Compatibility Mode" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">WHENEVER</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-sql-var.html" title="VAR">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg-sql-commands.html" title="34.14. Embedded SQL Commands">Up</a></td><th width="60%" align="center">34.14. Embedded SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-informix-compat.html" title="34.15. Informix Compatibility Mode">Next</a></td></tr></table><hr /></div><div class="refentry" id="ECPG-SQL-WHENEVER"><div class="titlepage"></div><div class="refnamediv"><h2>WHENEVER</h2><p>WHENEVER — specify the action to be taken when an SQL statement causes a specific class condition to be raised</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">
3 WHENEVER { NOT FOUND | SQLERROR | SQLWARNING } <em class="replaceable"><code>action</code></em>
4 </pre></div><div class="refsect1" id="id-1.7.5.20.19.3"><h2>Description</h2><p>
5 Define a behavior which is called on the special cases (Rows not
6 found, SQL warnings or errors) in the result of SQL execution.
7 </p></div><div class="refsect1" id="id-1.7.5.20.19.4"><h2>Parameters</h2><p>
8 See <a class="xref" href="ecpg-errors.html#ECPG-WHENEVER" title="34.8.1. Setting Callbacks">Section 34.8.1</a> for a description of the
10 </p></div><div class="refsect1" id="id-1.7.5.20.19.5"><h2>Examples</h2><pre class="programlisting">
11 EXEC SQL WHENEVER NOT FOUND CONTINUE;
12 EXEC SQL WHENEVER NOT FOUND DO BREAK;
13 EXEC SQL WHENEVER NOT FOUND DO CONTINUE;
14 EXEC SQL WHENEVER SQLWARNING SQLPRINT;
15 EXEC SQL WHENEVER SQLWARNING DO warn();
16 EXEC SQL WHENEVER SQLERROR sqlprint;
17 EXEC SQL WHENEVER SQLERROR CALL print2();
18 EXEC SQL WHENEVER SQLERROR DO handle_error("select");
19 EXEC SQL WHENEVER SQLERROR DO sqlnotice(NULL, NONO);
20 EXEC SQL WHENEVER SQLERROR DO sqlprint();
21 EXEC SQL WHENEVER SQLERROR GOTO error_label;
22 EXEC SQL WHENEVER SQLERROR STOP;
24 A typical application is the use of <code class="literal">WHENEVER NOT FOUND
25 BREAK</code> to handle looping through result sets:
26 </p><pre class="programlisting">
30 EXEC SQL CONNECT TO testdb AS con1;
31 EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
32 EXEC SQL ALLOCATE DESCRIPTOR d;
33 EXEC SQL DECLARE cur CURSOR FOR SELECT current_database(), 'hoge', 256;
36 /* when end of result set reached, break out of while loop */
37 EXEC SQL WHENEVER NOT FOUND DO BREAK;
41 EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d;
48 EXEC SQL DEALLOCATE DESCRIPTOR d;
49 EXEC SQL DISCONNECT ALL;
54 </p></div><div class="refsect1" id="id-1.7.5.20.19.6"><h2>Compatibility</h2><p>
55 <code class="command">WHENEVER</code> is specified in the SQL standard, but
56 most of the actions are PostgreSQL extensions.
57 </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-sql-var.html" title="VAR">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg-sql-commands.html" title="34.14. Embedded SQL Commands">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-informix-compat.html" title="34.15. Informix Compatibility Mode">Next</a></td></tr><tr><td width="40%" align="left" valign="top">VAR </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 34.15. <span class="productname">Informix</span> Compatibility Mode</td></tr></table></div></body></html>