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

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