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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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, log the error but don't fail the task
  38. and <b>abort</b> execution and transaction and fail task.</p>
  39. <p>
  40. <b>Proxies</b>. Some JDBC drivers (including the Oracle thin driver),
  41. use the JVM's proxy settings to route their JDBC operations to the database.
  42. Since Ant1.7, Ant running on Java1.5 or later defaults to
  43. <a href="../proxy.html">using
  44. the proxy settings of the operating system</a>.
  45. Accordingly, the OS proxy settings need to be valid, or Ant's proxy
  46. support disabled with <code>-noproxy</code> option.
  47. </p>
  48. <h3>Parameters</h3>
  49. <table border="1" cellpadding="2" cellspacing="0">
  50. <tr>
  51. <td width="12%" valign="top"><b>Attribute</b></td>
  52. <td width="78%" valign="top"><b>Description</b></td>
  53. <td width="10%" valign="top"><b>Required</b></td>
  54. </tr>
  55. <tr>
  56. <td width="12%" valign="top">driver</td>
  57. <td width="78%" valign="top">Class name of the jdbc driver</td>
  58. <td width="10%" valign="top">Yes</td>
  59. </tr>
  60. <tr>
  61. <td width="12%" valign="top">url</td>
  62. <td width="78%" valign="top">Database connection url</td>
  63. <td width="10%" valign="top">Yes</td>
  64. </tr>
  65. <tr>
  66. <td width="12%" valign="top">userid</td>
  67. <td width="78%" valign="top">Database user name</td>
  68. <td width="10%" valign="top">Yes</td>
  69. </tr>
  70. <tr>
  71. <td width="12%" valign="top">password</td>
  72. <td width="78%" valign="top">Database password</td>
  73. <td width="10%" valign="top">Yes</td>
  74. </tr>
  75. <tr>
  76. <td width="12%" valign="top">src</td>
  77. <td width="78%" valign="top">File containing SQL statements</td>
  78. <td width="10%" valign="top">Yes, unless statements enclosed within tags</td>
  79. </tr>
  80. <tr>
  81. <td valign="top">encoding</td>
  82. <td valign="top">The encoding of the files containing SQL statements</td>
  83. <td align="center">No - defaults to default JVM encoding</td>
  84. </tr>
  85. <tr>
  86. <td width="12%" valign="top">delimiter</td>
  87. <td width="78%" valign="top">String that separates SQL statements</td>
  88. <td width="10%" valign="top">No, default &quot;;&quot;</td>
  89. </tr>
  90. <tr>
  91. <td width="12%" valign="top">autocommit</td>
  92. <td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
  93. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  94. </tr>
  95. <tr>
  96. <td width="12%" valign="top">print</td>
  97. <td width="78%" valign="top">Print result sets from the statements (default false)</td>
  98. <td width="10%" valign="top">No, default &quot;false&quot;</td>
  99. </tr>
  100. <tr>
  101. <td width="12%" valign="top">showheaders</td>
  102. <td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
  103. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  104. </tr>
  105. <tr>
  106. <td width="12%" valign="top">showtrailers</td>
  107. <td width="78%" valign="top">Print trailer for number of rows affected (default true)</td>
  108. <td width="10%" valign="top">No, default &quot;true&quot;</td>
  109. </tr>
  110. <tr>
  111. <td width="12%" valign="top">output</td>
  112. <td width="78%" valign="top">Output file for result sets (defaults to System.out)
  113. <b>Since Ant 1.8</b> can specify any Resource that supports output (see
  114. <a href="../develop.html#set-magic">note</a>).
  115. </td>
  116. <td width="10%" valign="top">No (print to System.out by default)</td>
  117. </tr>
  118. <tr>
  119. <td valign="top">append</td>
  120. <td valign="top">whether output should be appended to or overwrite
  121. an existing file. Defaults to false.</td>
  122. <td align="center" valign="top">No, ignored if <i>output</i> does not
  123. specify a filesystem destination.</td>
  124. </tr>
  125. <tr>
  126. <td width="12%" valign="top">classpath</td>
  127. <td width="78%" valign="top">Classpath used to load driver</td>
  128. <td width="10%" valign="top">No (use system classpath)</td>
  129. </tr>
  130. <tr>
  131. <td width="12%" valign="top">classpathref</td>
  132. <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>
  133. <td width="10%" valign="top">No (use system classpath)</td>
  134. </tr>
  135. <tr>
  136. <td width="12%" valign="top">onerror</td>
  137. <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
  138. <td width="10%" valign="top">No, default &quot;abort&quot;</td>
  139. </tr>
  140. <tr>
  141. <td width="12%" valign="top">rdbms</td>
  142. <td width="78%" valign="top">Execute task only if this rdbms</td>
  143. <td width="10%" valign="top">No (no restriction)</td>
  144. </tr>
  145. <tr>
  146. <td width="12%" valign="top">version</td>
  147. <td width="78%" valign="top">Execute task only if rdbms version match</td>
  148. <td width="10%" valign="top">No (no restriction)</td>
  149. </tr>
  150. <tr>
  151. <td width="12%" valign="top">caching</td>
  152. <td width="78%" valign="top">Should the task cache loaders and the driver?</td>
  153. <td width="10%" valign="top">No (default=true)</td>
  154. </tr>
  155. <tr>
  156. <td width="12%" valign="top">delimitertype</td>
  157. <td width="78%" valign="top">Control whether the delimiter will only be recognized on a line by itself.<br>
  158. Can be "normal" -anywhere on the line, or "row", meaning it must be on a line by itself</td>
  159. <td width="10%" valign="top">No (default:normal)</td>
  160. </tr>
  161. <tr>
  162. <td width="12%" valign="top">keepformat</td>
  163. <td width="78%" valign="top">Control whether the format of the sql will be preserved.<br>
  164. Useful when loading packages and procedures.
  165. <td width="10%" valign="top">No (default=false)</td>
  166. </tr>
  167. <tr>
  168. <td width="12%" valign="top">escapeprocessing</td>
  169. <td width="78%" valign="top">Control whether the Java statement
  170. object will perform escape substitution.<br>
  171. See <a
  172. href="http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#setEscapeProcessing(boolean)">Statement's
  173. API docs</a> for details. <em>Since Ant 1.6</em>.
  174. <td width="10%" valign="top">No (default=true)</td>
  175. </tr>
  176. <tr>
  177. <td width="12%" valign="top">expandproperties</td>
  178. <td width="78%" valign="top">Set to true to turn on property expansion in
  179. nested SQL, inline in the task or nested transactions. <em>Since Ant 1.7</em>.
  180. <td width="10%" valign="top">No (default=true)</td>
  181. </tr>
  182. <tr>
  183. <td width="12%" valign="top">rawblobs</td>
  184. <td width="78%" valign="top">If true, will write raw streams rather than hex encoding when
  185. printing BLOB results. <em>Since Ant 1.7.1</em>.</td>
  186. <td width="10%" valign="top">No, default <em>false</em></td>
  187. </tr>
  188. <tr>
  189. <td width="12%" valign="top">failOnConnectionError</td>
  190. <td width="78%" valign="top">If false, will only print a warning
  191. message and not execute any statement if the task fails to connect
  192. to the database. <em>Since Ant 1.8.0</em>.</td>
  193. <td width="10%" valign="top">No, default <em>true</em></td>
  194. </tr>
  195. <tr>
  196. <td width="12%" valign="top">strictDelimiterMatching</td>
  197. <td width="78%" valign="top">If false, delimiters will be searched
  198. for in a case-insesitive manner (i.e. delimer="go" matches "GO")
  199. and surrounding whitespace will be ignored (delimter="go" matches
  200. "GO "). <em>Since Ant 1.8.0</em>.</td>
  201. <td width="10%" valign="top">No, default <em>true</em></td>
  202. </tr>
  203. <tr>
  204. <td width="12%" valign="top">showWarnings</td>
  205. <td width="78%" valign="top">If true, SQLWarnings will be logged at
  206. the WARN level. <em>Since Ant 1.8.0</em>.<br/>
  207. <b>Note:</b> even if the attribute is set to false, warnings that
  208. apply to the connection will be logged at the verbose level.</td>
  209. <td width="10%" valign="top">No, default <em>false</em></td>
  210. </tr>
  211. <tr>
  212. <td width="12%" valign="top">treatWarningsAsErrors</td>
  213. <td width="78%" valign="top">If true, SQLWarnings will be treated
  214. like errors - and the logic selected via the onError attribute
  215. applies.
  216. <em>Since Ant 1.8.0</em>.</td>
  217. <td width="10%" valign="top">No, default <em>false</em></td>
  218. </tr>
  219. <tr>
  220. <td width="12%" valign="top">csvColumnSeparator</td>
  221. <td width="78%" valign="top">The column separator used when printing
  222. the results.
  223. <em>Since Ant 1.8.0</em>.</td>
  224. <td width="10%" valign="top">No, default <em>','</em></td>
  225. </tr>
  226. <tr>
  227. <td width="12%" valign="top">csvQuoteCharacter</td>
  228. <td width="78%" valign="top">The character used to quote column
  229. values.<br/>
  230. If set, columns that contain either the column separator or the
  231. quote character itself will be surrounded by the quote character.
  232. The quote character itself will be doubled if it appears inside of
  233. the column's value.<br/>
  234. <b>Note:</b> BLOB values will never be quoted.
  235. <em>Since Ant 1.8.0</em>.</td>
  236. <td width="10%" valign="top">No, default is not set (i.e. no quoting
  237. ever occurs)</td>
  238. </tr>
  239. </table>
  240. <h3>Parameters specified as nested elements</h3>
  241. <h4>transaction</h4>
  242. <p>Use nested <code>&lt;transaction&gt;</code>
  243. elements to specify multiple blocks of commands to the executed
  244. executed in the same connection but different transactions. This
  245. is particularly useful when there are multiple files to execute
  246. on the same schema.</p>
  247. <table border="1" cellpadding="2" cellspacing="0">
  248. <tr>
  249. <td valign="top"><b>Attribute</b></td>
  250. <td valign="top"><b>Description</b></td>
  251. <td align="center" valign="top"><b>Required</b></td>
  252. </tr>
  253. <tr>
  254. <td valign="top">src</td>
  255. <td valign="top">File containing SQL statements</td>
  256. <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
  257. </tr>
  258. </table>
  259. <p>The <code>&lt;transaction&gt;</code> element supports any <a
  260. href="../CoreTypes/resources.html">resource</a> or single element
  261. resource collection as nested element to specify the resource
  262. containing the SQL statements.</p>
  263. <h4>any <a href="../CoreTypes/resources.html">resource</a> or resource
  264. collection</h4>
  265. <p>You can specify multiple sources via nested resource collection
  266. elements. Each resource of the collection will be run in a
  267. transaction of its own. Prior to Ant 1.7 only filesets were
  268. supported. Use a sort resource collection to get a predictable order
  269. of transactions. </p>
  270. <h4>classpath</h4>
  271. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
  272. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  273. <em>classpath</em> element. It is used to load the JDBC classes.</p>
  274. <h3>Examples</h3>
  275. <blockquote><pre>&lt;sql
  276. driver=&quot;org.database.jdbcDriver&quot;
  277. url=&quot;jdbc:database-url&quot;
  278. userid=&quot;sa&quot;
  279. password=&quot;pass&quot;
  280. src=&quot;data.sql&quot;
  281. /&gt;
  282. </pre></blockquote>
  283. <p>Connects to the database given in <i>url</i> as the sa user using the
  284. org.database.jdbcDriver and executes the SQL statements contained within
  285. the file data.sql</p>
  286. <blockquote><pre>&lt;sql
  287. driver=&quot;org.database.jdbcDriver&quot;
  288. url=&quot;jdbc:database-url&quot;
  289. userid=&quot;sa&quot;
  290. password=&quot;pass&quot;
  291. &gt;
  292. insert
  293. into table some_table
  294. values(1,2,3,4);
  295. truncate table some_other_table;
  296. &lt;/sql&gt;
  297. </pre></blockquote>
  298. <p>Connects to the database given in <i>url</i> as the sa
  299. user using the org.database.jdbcDriver and executes the two SQL statements
  300. inserting data into some_table and truncating some_other_table. Ant Properties
  301. in the nested text will not be expanded.</p>
  302. <p>Note that you may want to enclose your statements in
  303. <code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
  304. need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  305. or other special characters. For example:</p>
  306. <blockquote><pre>&lt;sql
  307. driver=&quot;org.database.jdbcDriver&quot;
  308. url=&quot;jdbc:database-url&quot;
  309. userid=&quot;sa&quot;
  310. password=&quot;pass&quot;
  311. &gt;&lt;![CDATA[
  312. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  313. ]]&gt;&lt;/sql&gt;
  314. </pre></blockquote>
  315. The following command turns property expansion in nested text on (it is off purely for backwards
  316. compatibility), then creates a new user in the HSQLDB database using Ant properties.
  317. <blockquote><pre>&lt;sql
  318. driver="org.hsqldb.jdbcDriver";
  319. url="jdbc:hsqldb:file:${database.dir}"
  320. userid="sa"
  321. password=""
  322. expandProperties="true"
  323. &gt;
  324. &lt;transaction&gt;
  325. CREATE USER ${newuser} PASSWORD ${newpassword}
  326. &lt;/transaction&gt;
  327. &lt;/sql&gt;
  328. </pre></blockquote>
  329. <p>The following connects to the database given in url as the sa user using
  330. the org.database.jdbcDriver and executes the SQL statements contained within
  331. the files data1.sql, data2.sql and data3.sql and then executes the truncate
  332. operation on <i>some_other_table</i>.</p>
  333. <blockquote><pre>&lt;sql
  334. driver=&quot;org.database.jdbcDriver&quot;
  335. url=&quot;jdbc:database-url&quot;
  336. userid=&quot;sa&quot;
  337. password=&quot;pass&quot; &gt;
  338. &lt;transaction src=&quot;data1.sql&quot;/&gt;
  339. &lt;transaction src=&quot;data2.sql&quot;/&gt;
  340. &lt;transaction src=&quot;data3.sql&quot;/&gt;
  341. &lt;transaction&gt;
  342. truncate table some_other_table;
  343. &lt;/transaction&gt;
  344. &lt;/sql&gt;
  345. </pre></blockquote>
  346. <p>The following example does the same as (and may execute additional
  347. SQL files if there are more files matching the pattern
  348. <code>data*.sql</code>) but doesn't guarantee that data1.sql will be
  349. run before <code>data2.sql</code>.</p>
  350. <blockquote><pre>&lt;sql
  351. driver=&quot;org.database.jdbcDriver&quot;
  352. url=&quot;jdbc:database-url&quot;
  353. userid=&quot;sa&quot;
  354. password=&quot;pass&quot;&gt;
  355. &lt;path&gt;
  356. &lt;fileset dir=&quot;.&quot;&gt;
  357. &lt;include name=&quot;data*.sql&quot;/&gt;
  358. &lt;/fileset&gt;
  359. &lt;/path&gt;
  360. &lt;transaction&gt;
  361. truncate table some_other_table;
  362. &lt;/transaction&gt;
  363. &lt;/sql&gt;
  364. </pre></blockquote>
  365. <p>The following connects to the database given in url as the sa user using the
  366. org.database.jdbcDriver and executes the SQL statements contained within the
  367. file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar
  368. as well as the system classpath for the driver class.</p>
  369. <blockquote><pre>&lt;sql
  370. driver=&quot;org.database.jdbcDriver&quot;
  371. url=&quot;jdbc:database-url&quot;
  372. userid=&quot;sa&quot;
  373. password=&quot;pass&quot;
  374. src=&quot;data.sql&quot;
  375. print=&quot;yes&quot;
  376. output=&quot;outputfile.txt&quot;
  377. &gt;
  378. &lt;classpath&gt;
  379. &lt;pathelement location=&quot;/some/jdbc.jar&quot;/&gt;
  380. &lt;/classpath&gt;
  381. &lt;/sql&gt;
  382. </pre></blockquote>
  383. <p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
  384. starts with &quot;8.1.&quot;</p>
  385. <blockquote><pre>&lt;sql
  386. driver=&quot;org.database.jdbcDriver&quot;
  387. url=&quot;jdbc:database-url&quot;
  388. userid=&quot;sa&quot;
  389. password=&quot;pass&quot;
  390. src=&quot;data.sql&quot;
  391. rdbms=&quot;oracle&quot;
  392. version=&quot;8.1.&quot;
  393. &gt;
  394. insert
  395. into table some_table
  396. values(1,2,3,4);
  397. truncate table some_other_table;
  398. &lt;/sql&gt;
  399. </pre></blockquote>
  400. </body>
  401. </html>