You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sql.html 8.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Language" content="en-us">
  4. <title>Ant User Manual</title>
  5. </head>
  6. <body>
  7. <h2><a name="sql">Sql</a></h2>
  8. <h3>Description</h3>
  9. <p>Executes a series of SQL statements via JDBC to a database. Statements can
  10. either be read in from a text file using the <i>src</i> attribute or from
  11. between the enclosing SQL tags.</p>
  12. <p>Multiple statements can be provided, separated by semicolons (or the
  13. defined <i>delimiter</i>). Individual lines within the statements can be
  14. commented using either --, // or REM at the start of the line.</p>
  15. <p>The <i>autocommit</i> attribute specifies whether auto-commit should be
  16. turned on or off whilst executing the statements. If auto-commit is turned
  17. on each statement will be executed and committed. If it is turned off the
  18. statements will all be executed as one transaction.</p>
  19. <p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
  20. during the execution of one of the statements.
  21. The possible values are: <b>continue</b> execution, only show the error;
  22. <b>stop</b> execution and commit transaction;
  23. and <b>abort</b> execution and transaction and fail task.</p>
  24. <h3>Parameters</h3>
  25. <table border="1" cellpadding="2" cellspacing="0">
  26. <tr>
  27. <td width="12%" valign="top"><b>Attribute</b></td>
  28. <td width="78%" valign="top"><b>Description</b></td>
  29. <td width="10%" valign="top"><b>Required</b></td>
  30. </tr>
  31. <tr>
  32. <td width="12%" valign="top">driver</td>
  33. <td width="78%" valign="top">Class name of the jdbc driver</td>
  34. <td width="10%" valign="top">Yes</td>
  35. </tr>
  36. <tr>
  37. <td width="12%" valign="top">url</td>
  38. <td width="78%" valign="top">Database connection url</td>
  39. <td width="10%" valign="top">Yes</td>
  40. </tr>
  41. <tr>
  42. <td width="12%" valign="top">userid</td>
  43. <td width="78%" valign="top">Database user name</td>
  44. <td width="10%" valign="top">Yes</td>
  45. </tr>
  46. <tr>
  47. <td width="12%" valign="top">password</td>
  48. <td width="78%" valign="top">Database password</td>
  49. <td width="10%" valign="top">Yes</td>
  50. </tr>
  51. <tr>
  52. <td width="12%" valign="top">src</td>
  53. <td width="78%" valign="top">File containing SQL statements</td>
  54. <td width="10%" valign="top">Yes, unless statements enclosed within tags</td>
  55. </tr>
  56. <tr>
  57. <td width="12%" valign="top">delimiter</td>
  58. <td width="78%" valign="top">String that separates SQL statements</td>
  59. <td width="10%" valign="top">No, default &quot;;&quot;</td>
  60. </tr>
  61. <tr>
  62. <td width="12%" valign="top">autocommit</td>
  63. <td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
  64. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  65. </tr>
  66. <tr>
  67. <td width="12%" valign="top">print</td>
  68. <td width="78%" valign="top">Print result sets from the statements (default false)</td>
  69. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  70. </tr>
  71. <tr>
  72. <td width="12%" valign="top">showheaders</td>
  73. <td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
  74. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  75. </tr>
  76. <tr>
  77. <td width="12%" valign="top">output</td>
  78. <td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
  79. <td width="10%" valign="top">No (print to System.out by default)</td>
  80. </tr>
  81. <tr>
  82. <td width="12%" valign="top">classpath</td>
  83. <td width="78%" valign="top">Classpath used to load driver</td>
  84. <td width="10%" valign="top">No (use system classpath)</td>
  85. </tr>
  86. <tr>
  87. <td width="12%" valign="top">onerror</td>
  88. <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
  89. <td width="10%" valign="top">No, default &quot;abort&quot;</td>
  90. </tr>
  91. <tr>
  92. <td width="12%" valign="top">rdbms</td>
  93. <td width="78%" valign="top">Execute task only if this rdbms</td>
  94. <td width="10%" valign="top">No (no restriction)</td>
  95. </tr>
  96. <tr>
  97. <td width="12%" valign="top">version</td>
  98. <td width="78%" valign="top">Execute task only if rdbms version match</td>
  99. <td width="10%" valign="top">No (no restriction)</td>
  100. </tr>
  101. </table>
  102. <h3>Parameters specified as nested elements</h3>
  103. <h4>transaction</h4>
  104. <p>Use nested <code>&lt;transaction&gt;</code>
  105. elements to specify multiple blocks of commands to the executed
  106. executed in the same connection but different transactions. This
  107. is particularly useful when there are multiple files to execute
  108. on the same schema.</p>
  109. <table border="1" cellpadding="2" cellspacing="0">
  110. <tr>
  111. <td valign="top"><b>Attribute</b></td>
  112. <td valign="top"><b>Description</b></td>
  113. <td align="center" valign="top"><b>Required</b></td>
  114. </tr>
  115. <tr>
  116. <td valign="top">src</td>
  117. <td valign="top">File containing SQL statements</td>
  118. <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
  119. </tr>
  120. </table>
  121. <h4>classpath</h4>
  122. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
  123. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  124. <em>classpath</em> element. It is used to load the JDBC classes.</p>
  125. <p>
  126. The
  127. </p>
  128. <h3>Examples</h3>
  129. <blockquote><pre>&lt;sql
  130. driver=&quot;org.database.jdbcDriver&quot;
  131. url=&quot;jdbc:database-url&quot;
  132. userid=&quot;sa&quot;
  133. password=&quot;pass&quot;
  134. src=&quot;data.sql&quot;
  135. /&gt;
  136. </pre></blockquote>
  137. <p>Connects to the database given in <i>url</i> as the sa user using the
  138. org.database.jdbcDriver and executes the SQL statements contained within
  139. the file data.sql</p>
  140. <blockquote><pre>&lt;sql
  141. driver=&quot;org.database.jdbcDriver&quot;
  142. url=&quot;jdbc:database-url&quot;
  143. userid=&quot;sa&quot;
  144. password=&quot;pass&quot;
  145. &gt;
  146. insert
  147. into table some_table
  148. values(1,2,3,4);
  149. truncate table some_other_table;
  150. &lt;/sql&gt;
  151. </pre></blockquote>
  152. <p>Connects to the database given in <i>url</i> as the sa
  153. user using the org.database.jdbcDriver and executes the two SQL statements
  154. inserting data into some_table and truncating some_other_table </p>
  155. <p>Note that you may want to enclose your statements in
  156. <code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
  157. need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  158. or other special characters. For example:</p>
  159. <blockquote><pre>&lt;sql
  160. driver=&quot;org.database.jdbcDriver&quot;
  161. url=&quot;jdbc:database-url&quot;
  162. userid=&quot;sa&quot;
  163. password=&quot;pass&quot;
  164. &gt;&lt;![CDATA[
  165. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  166. ]]&gt;&lt;/sql&gt;
  167. </pre></blockquote>
  168. <p>The following connects to the database given in url as the sa user using
  169. the org.database.jdbcDriver and executes the SQL statements contained within
  170. the files data1.sql, data2.sql and data3.sql and then executes the truncate
  171. operation on <i>some_other_table</i>.</p>
  172. <blockquote><pre>&lt;sql
  173. driver=&quot;org.database.jdbcDriver&quot;
  174. url=&quot;jdbc:database-url&quot;
  175. userid=&quot;sa&quot;
  176. password=&quot;pass&quot; &gt;
  177. &lt;transaction src=&quot;data1.sql&quot; /&gt;
  178. &lt;transaction src=&quot;data2.sql&quot; /&gt;
  179. &lt;transaction src=&quot;data3.sql&quot; /&gt;
  180. &lt;transaction&gt;
  181. truncate table some_other_table;
  182. &lt;/transaction&gt;
  183. &lt;/sql&gt;
  184. </pre></blockquote>
  185. <p>The following connects to the database given in url as the sa user using the
  186. org.database.jdbcDriver and executes the SQL statements contained within the
  187. file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar
  188. as well as the system classpath for the driver class.</p>
  189. <blockquote><pre>&lt;sql
  190. driver=&quot;org.database.jdbcDriver&quot;
  191. url=&quot;jdbc:database-url&quot;
  192. userid=&quot;sa&quot;
  193. password=&quot;pass&quot;
  194. src=&quot;data.sql&quot;
  195. print=&quot;yes&quot;
  196. output=&quot;outputfile.txt&quot;
  197. &gt;
  198. &lt;classpath&gt;
  199. &lt;pathelement location=&quot;/some/jdbc.jar&quot;&gt;
  200. &lt;/classpath&gt;
  201. &lt;/sql&gt;
  202. </pre></blockquote>
  203. <p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
  204. starts with &quot;8.1.&quot;</p>
  205. <blockquote><pre>&lt;sql
  206. driver=&quot;org.database.jdbcDriver&quot;
  207. url=&quot;jdbc:database-url&quot;
  208. userid=&quot;sa&quot;
  209. password=&quot;pass&quot;
  210. src=&quot;data.sql&quot;
  211. rdbms=&quot;oracle&quot;
  212. version=&quot;8.1.&quot;
  213. &gt;
  214. insert
  215. into table some_table
  216. values(1,2,3,4);
  217. truncate table some_other_table;
  218. &lt;/sql&gt;
  219. </pre></blockquote>
  220. </body>
  221. </html>