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 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <!--
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. -->
  15. <html>
  16. <head>
  17. <meta http-equiv="Content-Language" content="en-us">
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>SQL Task</title>
  20. </head>
  21. <body>
  22. <h2><a name="sql">Sql</a></h2>
  23. <h3>Description</h3>
  24. <p>Executes a series of SQL statements via JDBC to a database. Statements can
  25. either be read in from a text file using the <i>src</i> attribute or from
  26. between the enclosing SQL tags.</p>
  27. <p>Multiple statements can be provided, separated by semicolons (or the
  28. defined <i>delimiter</i>). Individual lines within the statements can be
  29. commented using either --, // or REM at the start of the line.</p>
  30. <p>The <i>autocommit</i> attribute specifies whether auto-commit should be
  31. turned on or off whilst executing the statements. If auto-commit is turned
  32. on each statement will be executed and committed. If it is turned off the
  33. statements will all be executed as one transaction.</p>
  34. <p>The <i>onerror</i> attribute specifies how to proceed when an error occurs
  35. during the execution of one of the statements.
  36. The possible values are: <b>continue</b> execution, only show the error;
  37. <b>stop</b> execution and commit transaction;
  38. and <b>abort</b> execution and transaction and fail task.</p>
  39. <h3>Parameters</h3>
  40. <table border="1" cellpadding="2" cellspacing="0">
  41. <tr>
  42. <td width="12%" valign="top"><b>Attribute</b></td>
  43. <td width="78%" valign="top"><b>Description</b></td>
  44. <td width="10%" valign="top"><b>Required</b></td>
  45. </tr>
  46. <tr>
  47. <td width="12%" valign="top">driver</td>
  48. <td width="78%" valign="top">Class name of the jdbc driver</td>
  49. <td width="10%" valign="top">Yes</td>
  50. </tr>
  51. <tr>
  52. <td width="12%" valign="top">url</td>
  53. <td width="78%" valign="top">Database connection url</td>
  54. <td width="10%" valign="top">Yes</td>
  55. </tr>
  56. <tr>
  57. <td width="12%" valign="top">userid</td>
  58. <td width="78%" valign="top">Database user name</td>
  59. <td width="10%" valign="top">Yes</td>
  60. </tr>
  61. <tr>
  62. <td width="12%" valign="top">password</td>
  63. <td width="78%" valign="top">Database password</td>
  64. <td width="10%" valign="top">Yes</td>
  65. </tr>
  66. <tr>
  67. <td width="12%" valign="top">src</td>
  68. <td width="78%" valign="top">File containing SQL statements</td>
  69. <td width="10%" valign="top">Yes, unless statements enclosed within tags</td>
  70. </tr>
  71. <tr>
  72. <td valign="top">encoding</td>
  73. <td valign="top">The encoding of the files containing SQL statements</td>
  74. <td align="center">No - defaults to default JVM encoding</td>
  75. </tr>
  76. <tr>
  77. <td width="12%" valign="top">delimiter</td>
  78. <td width="78%" valign="top">String that separates SQL statements</td>
  79. <td width="10%" valign="top">No, default &quot;;&quot;</td>
  80. </tr>
  81. <tr>
  82. <td width="12%" valign="top">autocommit</td>
  83. <td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
  84. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  85. </tr>
  86. <tr>
  87. <td width="12%" valign="top">print</td>
  88. <td width="78%" valign="top">Print result sets from the statements (default false)</td>
  89. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  90. </tr>
  91. <tr>
  92. <td width="12%" valign="top">showheaders</td>
  93. <td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
  94. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  95. </tr>
  96. <tr>
  97. <td width="12%" valign="top">showtrailers</td>
  98. <td width="78%" valign="top">Print trailer for number of rows affected (default true)</td>
  99. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  100. </tr>
  101. <tr>
  102. <td width="12%" valign="top">output</td>
  103. <td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
  104. <td width="10%" valign="top">No (print to System.out by default)</td>
  105. </tr>
  106. <tr>
  107. <td valign="top">append</td>
  108. <td valign="top">whether output should be appended to or overwrite
  109. an existing file. Defaults to false.</td>
  110. <td align="center" valign="top">No</td>
  111. </tr>
  112. <tr>
  113. <td width="12%" valign="top">classpath</td>
  114. <td width="78%" valign="top">Classpath used to load driver</td>
  115. <td width="10%" valign="top">No (use system classpath)</td>
  116. </tr>
  117. <tr>
  118. <td width="12%" valign="top">classpathref</td>
  119. <td width="78%" valign="top">The classpath to use, given as a <a href="../using.html#references">reference</a> to a path defined elsewhere.</td>
  120. <td width="10%" valign="top">No (use system classpath)</td>
  121. </tr>
  122. <tr>
  123. <td width="12%" valign="top">onerror</td>
  124. <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
  125. <td width="10%" valign="top">No, default &quot;abort&quot;</td>
  126. </tr>
  127. <tr>
  128. <td width="12%" valign="top">rdbms</td>
  129. <td width="78%" valign="top">Execute task only if this rdbms</td>
  130. <td width="10%" valign="top">No (no restriction)</td>
  131. </tr>
  132. <tr>
  133. <td width="12%" valign="top">version</td>
  134. <td width="78%" valign="top">Execute task only if rdbms version match</td>
  135. <td width="10%" valign="top">No (no restriction)</td>
  136. </tr>
  137. <tr>
  138. <td width="12%" valign="top">caching</td>
  139. <td width="78%" valign="top">Should the task cache loaders and the driver?</td>
  140. <td width="10%" valign="top">No (default=true)</td>
  141. </tr>
  142. <tr>
  143. <td width="12%" valign="top">delimitertype</td>
  144. <td width="78%" valign="top">Control whether the delimiter will only be recognized on a line by itself.<br>
  145. Can be "normal" -anywhere on the line, or "row", meaning it must be on a line by itself</td>
  146. <td width="10%" valign="top">No (default:normal)</td>
  147. </tr>
  148. <tr>
  149. <td width="12%" valign="top">keepformat</td>
  150. <td width="78%" valign="top">Control whether the format of the sql will be preserved.<br>
  151. Usefull when loading packages and procedures.
  152. <td width="10%" valign="top">No (default=false)</td>
  153. </tr>
  154. <tr>
  155. <td width="12%" valign="top">escapeprocessing</td>
  156. <td width="78%" valign="top">Control whether the Java statement
  157. object will perform escape substitution.<br>
  158. See <a
  159. href="http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#setEscapeProcessing(boolean)">Statement's
  160. API docs</a> for details. <em>Since Ant 1.6</em>.
  161. <td width="10%" valign="top">No (default=true)</td>
  162. </tr>
  163. <tr>
  164. <td width="12%" valign="top">expandproperties</td>
  165. <td width="78%" valign="top">Set to true to turn on property expansion in
  166. nested SQL, inline in the task or nested transactions. <em>Since Ant 1.7</em>.
  167. <td width="10%" valign="top">No (default=false)</td>
  168. </tr>
  169. </table>
  170. <h3>Parameters specified as nested elements</h3>
  171. <h4>transaction</h4>
  172. <p>Use nested <code>&lt;transaction&gt;</code>
  173. elements to specify multiple blocks of commands to the executed
  174. executed in the same connection but different transactions. This
  175. is particularly useful when there are multiple files to execute
  176. on the same schema.</p>
  177. <table border="1" cellpadding="2" cellspacing="0">
  178. <tr>
  179. <td valign="top"><b>Attribute</b></td>
  180. <td valign="top"><b>Description</b></td>
  181. <td align="center" valign="top"><b>Required</b></td>
  182. </tr>
  183. <tr>
  184. <td valign="top">src</td>
  185. <td valign="top">File containing SQL statements</td>
  186. <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
  187. </tr>
  188. </table>
  189. <p>The <code>&lt;transaction&gt;</code> element supports any <a
  190. href="../CoreTypes/resources.html">resource</a> or single element
  191. resource collection as nested element to specify the resource
  192. containing the SQL statements.</p>
  193. <h4>any <a href="../CoreTypes/resources.html">resource</a> or resource
  194. collection</h4>
  195. <p>You can specify multiple sources via nested resource collection
  196. elements. Each resource of the collection will be run in a
  197. transaction of its own. Prior to Ant 1.7 only filesets were
  198. supported. Use a sort resource collection to get a predictable order
  199. of transactions. </p>
  200. <h4>classpath</h4>
  201. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
  202. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  203. <em>classpath</em> element. It is used to load the JDBC classes.</p>
  204. <h3>Examples</h3>
  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. /&gt;
  212. </pre></blockquote>
  213. <p>Connects to the database given in <i>url</i> as the sa user using the
  214. org.database.jdbcDriver and executes the SQL statements contained within
  215. the file data.sql</p>
  216. <blockquote><pre>&lt;sql
  217. driver=&quot;org.database.jdbcDriver&quot;
  218. url=&quot;jdbc:database-url&quot;
  219. userid=&quot;sa&quot;
  220. password=&quot;pass&quot;
  221. &gt;
  222. insert
  223. into table some_table
  224. values(1,2,3,4);
  225. truncate table some_other_table;
  226. &lt;/sql&gt;
  227. </pre></blockquote>
  228. <p>Connects to the database given in <i>url</i> as the sa
  229. user using the org.database.jdbcDriver and executes the two SQL statements
  230. inserting data into some_table and truncating some_other_table. Ant Properties
  231. in the nested text will not be expanded.</p>
  232. <p>Note that you may want to enclose your statements in
  233. <code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
  234. need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  235. or other special characters. For example:</p>
  236. <blockquote><pre>&lt;sql
  237. driver=&quot;org.database.jdbcDriver&quot;
  238. url=&quot;jdbc:database-url&quot;
  239. userid=&quot;sa&quot;
  240. password=&quot;pass&quot;
  241. &gt;&lt;![CDATA[
  242. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  243. ]]&gt;&lt;/sql&gt;
  244. </pre></blockquote>
  245. The following command turns property expansion in nested text on (it is off purely for backwards
  246. compatibility), then creates a new user in the HSQLDB database using Ant properties.
  247. <blockquote><pre>&lt;sql
  248. driver="org.hsqldb.jdbcDriver";
  249. url="jdbc:hsqldb:file:${database.dir}"
  250. userid="sa"
  251. password=""
  252. expandProperties="true"
  253. &gt;
  254. &lt;transaction&gt;
  255. CREATE USER ${newuser} PASSWORD ${newpassword}
  256. &lt;/transaction&gt;
  257. &lt;/sql&gt;
  258. </pre></blockquote>
  259. <p>The following connects to the database given in url as the sa user using
  260. the org.database.jdbcDriver and executes the SQL statements contained within
  261. the files data1.sql, data2.sql and data3.sql and then executes the truncate
  262. operation on <i>some_other_table</i>.</p>
  263. <blockquote><pre>&lt;sql
  264. driver=&quot;org.database.jdbcDriver&quot;
  265. url=&quot;jdbc:database-url&quot;
  266. userid=&quot;sa&quot;
  267. password=&quot;pass&quot; &gt;
  268. &lt;transaction src=&quot;data1.sql&quot;/&gt;
  269. &lt;transaction src=&quot;data2.sql&quot;/&gt;
  270. &lt;transaction src=&quot;data3.sql&quot;/&gt;
  271. &lt;transaction&gt;
  272. truncate table some_other_table;
  273. &lt;/transaction&gt;
  274. &lt;/sql&gt;
  275. </pre></blockquote>
  276. <p>The following example does the same as (and may execute additional
  277. SQL files if there are more files matching the pattern
  278. <code>data*.sql</code>) but doesn't guarantee that data1.sql will be
  279. run before <code>data2.sql</code>.</p>
  280. <blockquote><pre>&lt;sql
  281. driver=&quot;org.database.jdbcDriver&quot;
  282. url=&quot;jdbc:database-url&quot;
  283. userid=&quot;sa&quot;
  284. password=&quot;pass&quot;&gt;
  285. &lt;path&gt;
  286. &lt;fileset dir=&quot;.&quot;&gt;
  287. &lt;include name=&quot;data*.sql&quot;/&gt;
  288. &lt;/fileset&gt;
  289. &lt;path&gt;
  290. &lt;transaction&gt;
  291. truncate table some_other_table;
  292. &lt;/transaction&gt;
  293. &lt;/sql&gt;
  294. </pre></blockquote>
  295. <p>The following connects to the database given in url as the sa user using the
  296. org.database.jdbcDriver and executes the SQL statements contained within the
  297. file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar
  298. as well as the system classpath for the driver class.</p>
  299. <blockquote><pre>&lt;sql
  300. driver=&quot;org.database.jdbcDriver&quot;
  301. url=&quot;jdbc:database-url&quot;
  302. userid=&quot;sa&quot;
  303. password=&quot;pass&quot;
  304. src=&quot;data.sql&quot;
  305. print=&quot;yes&quot;
  306. output=&quot;outputfile.txt&quot;
  307. &gt;
  308. &lt;classpath&gt;
  309. &lt;pathelement location=&quot;/some/jdbc.jar&quot;/&gt;
  310. &lt;/classpath&gt;
  311. &lt;/sql&gt;
  312. </pre></blockquote>
  313. <p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
  314. starts with &quot;8.1.&quot;</p>
  315. <blockquote><pre>&lt;sql
  316. driver=&quot;org.database.jdbcDriver&quot;
  317. url=&quot;jdbc:database-url&quot;
  318. userid=&quot;sa&quot;
  319. password=&quot;pass&quot;
  320. src=&quot;data.sql&quot;
  321. rdbms=&quot;oracle&quot;
  322. version=&quot;8.1.&quot;
  323. &gt;
  324. insert
  325. into table some_table
  326. values(1,2,3,4);
  327. truncate table some_other_table;
  328. &lt;/sql&gt;
  329. </pre></blockquote>
  330. </body>
  331. </html>