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

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