gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
4.5KB

  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 V1.79.1" /><link rel="prev" href="ecpg-sql-var.html" title="VAR" /><link rel="next" href="ecpg-informix-compat.html" title="35.15. Informix Compatibility Mode" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" 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="35.14. Embedded SQL Commands">Up</a></td><th width="60%" align="center">35.14. Embedded SQL Commands</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="ecpg-informix-compat.html" title="35.15. Informix Compatibility Mode">Next</a></td></tr></table><hr></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.18.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.18.4"><h2>Parameters</h2><p>
  8. See <a class="xref" href="ecpg-errors.html#ECPG-WHENEVER" title="35.8.1. Setting Callbacks">Section 35.8.1</a> for a description of the
  9. parameters.
  10. </p></div><div class="refsect1" id="id-1.7.5.20.18.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;
  23. </pre><p>
  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">
  27. int
  28. main(void)
  29. {
  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;
  34. EXEC SQL OPEN cur;
  35. /* when end of result set reached, break out of while loop */
  36. EXEC SQL WHENEVER NOT FOUND DO BREAK;
  37. while (1)
  38. {
  39. EXEC SQL FETCH NEXT FROM cur INTO SQL DESCRIPTOR d;
  40. ...
  41. }
  42. EXEC SQL CLOSE cur;
  43. EXEC SQL COMMIT;
  44. EXEC SQL DEALLOCATE DESCRIPTOR d;
  45. EXEC SQL DISCONNECT ALL;
  46. return 0;
  47. }
  48. </pre><p>
  49. </p></div><div class="refsect1" id="id-1.7.5.20.18.6"><h2>Compatibility</h2><p>
  50. <code class="command">WHENEVER</code> is specified in the SQL standard, but
  51. most of the actions are PostgreSQL extensions.
  52. </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">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg-sql-commands.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-informix-compat.html">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">Home</a></td><td width="40%" align="right" valign="top"> 35.15. <span class="productname">Informix</span> Compatibility Mode</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1