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.

103 lines
7.0KB

  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>35.5. Dynamic SQL</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-variables.html" title="35.4. Using Host Variables" /><link rel="next" href="ecpg-pgtypes.html" title="35.6. pgtypes Library" /></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">35.5. Dynamic SQL</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="ecpg-variables.html" title="35.4. Using Host Variables">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="ecpg.html" title="Chapter 35. ECPG - Embedded SQL in C">Up</a></td><th width="60%" align="center">Chapter 35. <span xmlns="http://www.w3.org/1999/xhtml" class="application">ECPG</span> - Embedded <acronym xmlns="http://www.w3.org/1999/xhtml" class="acronym">SQL</acronym> in C</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-pgtypes.html" title="35.6. pgtypes Library">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="ECPG-DYNAMIC"><div class="titlepage"><div><div><h2 class="title" style="clear: both">35.5. Dynamic SQL</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="ecpg-dynamic.html#ECPG-DYNAMIC-WITHOUT-RESULT">35.5.1. Executing Statements without a Result Set</a></span></dt><dt><span class="sect2"><a href="ecpg-dynamic.html#ECPG-DYNAMIC-INPUT">35.5.2. Executing a Statement with Input Parameters</a></span></dt><dt><span class="sect2"><a href="ecpg-dynamic.html#ECPG-DYNAMIC-WITH-RESULT">35.5.3. Executing a Statement with a Result Set</a></span></dt></dl></div><p>
  3. In many cases, the particular SQL statements that an application
  4. has to execute are known at the time the application is written.
  5. In some cases, however, the SQL statements are composed at run time
  6. or provided by an external source. In these cases you cannot embed
  7. the SQL statements directly into the C source code, but there is a
  8. facility that allows you to call arbitrary SQL statements that you
  9. provide in a string variable.
  10. </p><div class="sect2" id="ECPG-DYNAMIC-WITHOUT-RESULT"><div class="titlepage"><div><div><h3 class="title">35.5.1. Executing Statements without a Result Set</h3></div></div></div><p>
  11. The simplest way to execute an arbitrary SQL statement is to use
  12. the command <code class="command">EXECUTE IMMEDIATE</code>. For example:
  13. </p><pre class="programlisting">
  14. EXEC SQL BEGIN DECLARE SECTION;
  15. const char *stmt = "CREATE TABLE test1 (...);";
  16. EXEC SQL END DECLARE SECTION;
  17. EXEC SQL EXECUTE IMMEDIATE :stmt;
  18. </pre><p>
  19. <code class="command">EXECUTE IMMEDIATE</code> can be used for SQL
  20. statements that do not return a result set (e.g.,
  21. DDL, <code class="command">INSERT</code>, <code class="command">UPDATE</code>,
  22. <code class="command">DELETE</code>). You cannot execute statements that
  23. retrieve data (e.g., <code class="command">SELECT</code>) this way. The
  24. next section describes how to do that.
  25. </p></div><div class="sect2" id="ECPG-DYNAMIC-INPUT"><div class="titlepage"><div><div><h3 class="title">35.5.2. Executing a Statement with Input Parameters</h3></div></div></div><p>
  26. A more powerful way to execute arbitrary SQL statements is to
  27. prepare them once and execute the prepared statement as often as
  28. you like. It is also possible to prepare a generalized version of
  29. a statement and then execute specific versions of it by
  30. substituting parameters. When preparing the statement, write
  31. question marks where you want to substitute parameters later. For
  32. example:
  33. </p><pre class="programlisting">
  34. EXEC SQL BEGIN DECLARE SECTION;
  35. const char *stmt = "INSERT INTO test1 VALUES(?, ?);";
  36. EXEC SQL END DECLARE SECTION;
  37. EXEC SQL PREPARE mystmt FROM :stmt;
  38. ...
  39. EXEC SQL EXECUTE mystmt USING 42, 'foobar';
  40. </pre><p>
  41. </p><p>
  42. When you don't need the prepared statement anymore, you should
  43. deallocate it:
  44. </p><pre class="programlisting">
  45. EXEC SQL DEALLOCATE PREPARE <em class="replaceable"><code>name</code></em>;
  46. </pre><p>
  47. </p></div><div class="sect2" id="ECPG-DYNAMIC-WITH-RESULT"><div class="titlepage"><div><div><h3 class="title">35.5.3. Executing a Statement with a Result Set</h3></div></div></div><p>
  48. To execute an SQL statement with a single result row,
  49. <code class="command">EXECUTE</code> can be used. To save the result, add
  50. an <code class="literal">INTO</code> clause.
  51. </p><pre class="programlisting">
  52. EXEC SQL BEGIN DECLARE SECTION;
  53. const char *stmt = "SELECT a, b, c FROM test1 WHERE a &gt; ?";
  54. int v1, v2;
  55. VARCHAR v3[50];
  56. EXEC SQL END DECLARE SECTION;
  57. EXEC SQL PREPARE mystmt FROM :stmt;
  58. ...
  59. EXEC SQL EXECUTE mystmt INTO :v1, :v2, :v3 USING 37;
  60. </pre><p>
  61. An <code class="command">EXECUTE</code> command can have an
  62. <code class="literal">INTO</code> clause, a <code class="literal">USING</code> clause,
  63. both, or neither.
  64. </p><p>
  65. If a query is expected to return more than one result row, a
  66. cursor should be used, as in the following example.
  67. (See <a class="xref" href="ecpg-commands.html#ECPG-CURSORS" title="35.3.2. Using Cursors">Section 35.3.2</a> for more details about the
  68. cursor.)
  69. </p><pre class="programlisting">
  70. EXEC SQL BEGIN DECLARE SECTION;
  71. char dbaname[128];
  72. char datname[128];
  73. char *stmt = "SELECT u.usename as dbaname, d.datname "
  74. " FROM pg_database d, pg_user u "
  75. " WHERE d.datdba = u.usesysid";
  76. EXEC SQL END DECLARE SECTION;
  77. EXEC SQL CONNECT TO testdb AS con1 USER testuser;
  78. EXEC SQL SELECT pg_catalog.set_config('search_path', '', false); EXEC SQL COMMIT;
  79. EXEC SQL PREPARE stmt1 FROM :stmt;
  80. EXEC SQL DECLARE cursor1 CURSOR FOR stmt1;
  81. EXEC SQL OPEN cursor1;
  82. EXEC SQL WHENEVER NOT FOUND DO BREAK;
  83. while (1)
  84. {
  85. EXEC SQL FETCH cursor1 INTO :dbaname,:datname;
  86. printf("dbaname=%s, datname=%s\n", dbaname, datname);
  87. }
  88. EXEC SQL CLOSE cursor1;
  89. EXEC SQL COMMIT;
  90. EXEC SQL DISCONNECT ALL;
  91. </pre><p>
  92. </p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ecpg-variables.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ecpg.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ecpg-pgtypes.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">35.4. Using Host Variables </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 35.6. pgtypes Library</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1