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.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 statement via JDBC to a database. Statements can either be read in from a text file using the src attribute or from between the enclosing sql tags.</p>
  10. <p>Multiple statements can be set and each statement is delimited from the next use a semi-colon. Individual lines within the statements can be commented using either --, // or REM at the start of the line.</p>
  11. <p>The auto-commit 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">autocommit</td>
  50. <td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
  51. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  52. </tr>
  53. <tr>
  54. <td width="12%" valign="top">print</td>
  55. <td width="78%" valign="top">Print result sets from the statements (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">showheaders</td>
  60. <td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
  61. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  62. </tr>
  63. <tr>
  64. <td width="12%" valign="top">output</td>
  65. <td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
  66. <td width="10%" valign="top">No (print to System.out by default)</td>
  67. </tr>
  68. <tr>
  69. <td width="12%" valign="top">classpath</td>
  70. <td width="78%" valign="top">Classpath used to load driver</td>
  71. <td width="10%" valign="top">No (use system classpath)</td>
  72. </tr>
  73. <tr>
  74. <td width="12%" valign="top">onerror</td>
  75. <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
  76. <td width="10%" valign="top">No, default &quot;abort&quot;</td>
  77. </tr>
  78. <tr>
  79. <td width="12%" valign="top">rdbms</td>
  80. <td width="78%" valign="top">Execute task only if this rdbms</td>
  81. <td width="10%" valign="top">No (no restriction)</td>
  82. </tr>
  83. <tr>
  84. <td width="12%" valign="top">version</td>
  85. <td width="78%" valign="top">Execute task only if rdbms version match</td>
  86. <td width="10%" valign="top">No (no restriction)</td>
  87. </tr>
  88. </table>
  89. <h3>Parameters specified as nested elements</h3>
  90. <h4>transaction</h4>
  91. <p>Use nested <code>&lt;transaction&gt;</code>
  92. elements to specify multiple blocks of commands to the executed
  93. executed in the same connection but different transactions. This
  94. is particularly useful when there are multiple files to execute
  95. on the same schema.</p>
  96. <table border="1" cellpadding="2" cellspacing="0">
  97. <tr>
  98. <td valign="top"><b>Attribute</b></td>
  99. <td valign="top"><b>Description</b></td>
  100. <td align="center" valign="top"><b>Required</b></td>
  101. </tr>
  102. <tr>
  103. <td valign="top">src</td>
  104. <td valign="top">File containing sql statements</td>
  105. <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
  106. </tr>
  107. </table>
  108. <h4>classpath</h4>
  109. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
  110. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  111. <em>classpath</em> element. It is used to load the JDBC classes.</p>
  112. <p>
  113. The
  114. </p>
  115. <h3>Examples</h3>
  116. <blockquote><pre>&lt;sql
  117. driver=&quot;org.database.jdbcDriver&quot;
  118. url=&quot;jdbc:database-url&quot;
  119. userid=&quot;sa&quot;
  120. password=&quot;pass&quot;
  121. src=&quot;data.sql&quot;
  122. /&gt;
  123. </pre></blockquote>
  124. <p>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</p>
  125. <blockquote><pre>&lt;sql
  126. driver=&quot;org.database.jdbcDriver&quot;
  127. url=&quot;jdbc:database-url&quot;
  128. userid=&quot;sa&quot;
  129. password=&quot;pass&quot;
  130. &gt;
  131. insert
  132. into table some_table
  133. values(1,2,3,4);
  134. truncate table some_other_table;
  135. &lt;/sql&gt;
  136. </pre></blockquote>
  137. <p>Connects to the database given in url as the sa user using the org.database.jdbcDriver and executes the two sql statements inserting data into some_table and truncating some_other_table</p>
  138. <p>Note that you may want to enclose your statements in
  139. <code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
  140. need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  141. or other special characters. For example:</p>
  142. <blockquote><pre>&lt;sql
  143. driver=&quot;org.database.jdbcDriver&quot;
  144. url=&quot;jdbc:database-url&quot;
  145. userid=&quot;sa&quot;
  146. password=&quot;pass&quot;
  147. &gt;&lt;![CDATA[
  148. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  149. ]]&gt;&lt;/sql&gt;
  150. </pre></blockquote>
  151. <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>
  152. <blockquote><pre>&lt;sql
  153. driver=&quot;org.database.jdbcDriver&quot;
  154. url=&quot;jdbc:database-url&quot;
  155. userid=&quot;sa&quot;
  156. password=&quot;pass&quot; &gt;
  157. &lt;transaction src=&quot;data1.sql&quot; /&gt;
  158. &lt;transaction src=&quot;data2.sql&quot; /&gt;
  159. &lt;transaction src=&quot;data3.sql&quot; /&gt;
  160. &lt;transaction&gt;
  161. truncate table some_other_table;
  162. &lt;/transaction&gt;
  163. &lt;/sql&gt;
  164. </pre></blockquote>
  165. <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>
  166. <blockquote><pre>&lt;sql
  167. driver=&quot;org.database.jdbcDriver&quot;
  168. url=&quot;jdbc:database-url&quot;
  169. userid=&quot;sa&quot;
  170. password=&quot;pass&quot;
  171. src=&quot;data.sql&quot;
  172. print=&quot;yes&quot;
  173. output=&quot;outputfile.txt&quot;
  174. &gt;
  175. &lt;classpath&gt;
  176. &lt;pathelement location=&quot;/some/jdbc.jar&quot;&gt;
  177. &lt;/classpath&gt;
  178. &lt;/sql&gt;
  179. </pre></blockquote>
  180. <p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
  181. starts with &quot;8.1.&quot;</p>
  182. <blockquote><pre>&lt;sql
  183. driver=&quot;org.database.jdbcDriver&quot;
  184. url=&quot;jdbc:database-url&quot;
  185. userid=&quot;sa&quot;
  186. password=&quot;pass&quot;
  187. src=&quot;data.sql&quot;
  188. rdbms=&quot;oracle&quot;
  189. version=&quot;8.1.&quot;
  190. &gt;
  191. insert
  192. into table some_table
  193. values(1,2,3,4);
  194. truncate table some_other_table;
  195. &lt;/sql&gt;
  196. </pre></blockquote>
  197. </body>
  198. </html>