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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <!DOCTYPE html>
  2. <!--
  3. Licensed to the Apache Software Foundation (ASF) under one or more
  4. contributor license agreements. See the NOTICE file distributed with
  5. this work for additional information regarding copyright ownership.
  6. The ASF licenses this file to You under the Apache License, Version 2.0
  7. (the "License"); you may not use this file except in compliance with
  8. the License. You may obtain a copy of the License at
  9. https://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. -->
  16. <html lang="en">
  17. <head>
  18. <link rel="stylesheet" type="text/css" href="../stylesheets/style.css">
  19. <title>SQL Task</title>
  20. </head>
  21. <body>
  22. <h2 id="sql">Sql</h2>
  23. <h3>Description</h3>
  24. <p>Executes a series of SQL statements via JDBC to a database. Statements can either be read in from
  25. a text file using the <var>src</var> attribute or from between the enclosing SQL tags.</p>
  26. <p>Multiple statements can be provided, separated by semicolons (or the
  27. defined <var>delimiter</var>). Individual lines within the statements can be commented using
  28. either <q>--</q>, <q>//</q> or <q>REM</q> at the start of the line.</p>
  29. <p>The <var>autocommit</var> attribute specifies whether auto-commit should be turned on or off
  30. whilst executing the statements. If auto-commit is turned <q>on</q> each statement will be executed
  31. and committed. If it is turned <q>off</q> the statements will all be executed as one
  32. transaction.</p>
  33. <p>The <var>onerror</var> attribute specifies how to proceed when an error occurs during the
  34. execution of one of the statements. The possible values are: <q>continue</q> execution, only show
  35. the error; <q>stop</q> execution, log the error but don't fail the task and <q>abort</q> execution
  36. and transaction and fail task.</p>
  37. <p><strong>Proxies</strong>. Some JDBC drivers (including the Oracle thin driver), use the JVM's
  38. proxy settings to route their JDBC operations to the database. <em>Since Apache Ant 1.7</em>, Ant
  39. running on Java 5 or later defaults to <a href="../proxy.html">using the proxy settings of the
  40. operating system</a>. Accordingly, the OS proxy settings need to be valid, or Ant's proxy support
  41. disabled with <kbd>-noproxy</kbd> option.</p>
  42. <h3>Parameters</h3>
  43. <table class="attr">
  44. <tr>
  45. <th scope="col">Attribute</th>
  46. <th scope="col">Description</th>
  47. <th scope="col">Required</th>
  48. </tr>
  49. <tr>
  50. <td>driver</td>
  51. <td>Class name of the jdbc driver</td>
  52. <td>Yes</td>
  53. </tr>
  54. <tr>
  55. <td>url</td>
  56. <td>Database connection URL</td>
  57. <td>Yes</td>
  58. </tr>
  59. <tr>
  60. <td>userid</td>
  61. <td>Database user name</td>
  62. <td>Yes</td>
  63. </tr>
  64. <tr>
  65. <td>password</td>
  66. <td>Database password</td>
  67. <td>Yes</td>
  68. </tr>
  69. <tr>
  70. <td>src</td>
  71. <td>File containing SQL statements</td>
  72. <td>Yes, unless statements enclosed within tags</td>
  73. </tr>
  74. <tr>
  75. <td>encoding</td>
  76. <td>The encoding of the files containing SQL statements</td>
  77. <td>No; defaults to default JVM character encoding</td>
  78. </tr>
  79. <tr>
  80. <td>outputencoding</td>
  81. <td>The encoding of the files holding results. <em>since Ant 1.9.4</em></td>
  82. <td>No; defaults to default JVM character encoding</td>
  83. </tr>
  84. <tr>
  85. <td>delimiter</td>
  86. <td>String that separates SQL statements</td>
  87. <td>No; defaults to <q>;;</q></td>
  88. </tr>
  89. <tr>
  90. <td>autocommit</td>
  91. <td>Auto commit flag for database connection</td>
  92. <td>No; defaults to <q>false</q></td>
  93. </tr>
  94. <tr>
  95. <td>print</td>
  96. <td>Print result sets from the statements</td>
  97. <td>No; defaults to <q>false</q></td>
  98. </tr>
  99. <tr>
  100. <td>showheaders</td>
  101. <td>Print headers for result sets from the statements</td>
  102. <td>No; defaults to <q>true</q></td>
  103. </tr>
  104. <tr>
  105. <td>showtrailers</td>
  106. <td>Print trailer for number of rows affected</td>
  107. <td>No; defaults to <q>true</q></td>
  108. </tr>
  109. <tr>
  110. <td>output</td>
  111. <td>Output file for result sets. <em>Since Ant 1.8</em> can specify any Resource that supports
  112. output (see <a href="../develop.html#set-magic">note</a>).</td>
  113. <td>No; print to <code>System.out</code> by default</td>
  114. </tr>
  115. <tr>
  116. <td>append</td>
  117. <td>whether output should be appended to or overwrite an existing file.</td>
  118. <td>No; defaults to <q>false</q>, ignored if <var>output</var> does not specify a filesystem
  119. destination</td>
  120. </tr>
  121. <tr>
  122. <td>classpath</td>
  123. <td>Classpath used to load driver</td>
  124. <td>No; defaults to system classpath</td>
  125. </tr>
  126. <tr>
  127. <td>classpathref</td>
  128. <td>The classpath to use, given as a <a href="../using.html#references">reference</a> to a path
  129. defined elsewhere.</td>
  130. <td>No; defaults to system classpath</td>
  131. </tr>
  132. <tr>
  133. <td>onerror</td>
  134. <td>Action to perform when statement fails: <q>continue</q>, <q>stop</q>, <q>abort</q></td>
  135. <td>No; defaults to <q>abort</q></td>
  136. </tr>
  137. <tr>
  138. <td>rdbms</td>
  139. <td>Execute task only if this rdbms</td>
  140. <td>No; unrestricted by default</td>
  141. </tr>
  142. <tr>
  143. <td>version</td>
  144. <td>Execute task only if rdbms version match</td>
  145. <td>No; unrestricted by default</td>
  146. </tr>
  147. <tr>
  148. <td>caching</td>
  149. <td>Should the task cache loaders and the driver?</td>
  150. <td>No; defaults to <q>true</q></td>
  151. </tr>
  152. <tr>
  153. <td>delimitertype</td>
  154. <td>Control whether the delimiter will only be recognized on a line by itself.<br/>Can
  155. be <q>normal</q>&mdash;anywhere on the line, or <q>row</q>, meaning it must be on a line by
  156. itself</td>
  157. <td>No; defaults to <q>normal</q></td>
  158. </tr>
  159. <tr>
  160. <td>keepformat</td>
  161. <td>Control whether the format of SQL will be preserved.<br/>Useful when loading packages and
  162. procedures.
  163. <td>No; defaults to <q>false</q></td>
  164. </tr>
  165. <tr>
  166. <td>escapeprocessing</td>
  167. <td>Control whether the Java statement object will perform escape substitution.<br/>
  168. See <a href="https://docs.oracle.com/javase/8/docs/api/java/sql/Statement.html#setEscapeProcessing-boolean-"
  169. target="_top">Statement's API docs</a> for details. <em>Since Ant 1.6</em>.
  170. <td>No; defaults to <q>true</q></td>
  171. </tr>
  172. <tr>
  173. <td>expandproperties</td>
  174. <td>Set to true to turn on property expansion in nested SQL, inline in the task or nested
  175. transactions. <em>Since Ant 1.7</em>.
  176. <td>No; defaults to <q>true</q></td>
  177. </tr>
  178. <tr>
  179. <td>rawblobs</td>
  180. <td>If <q>true</q>, will write raw streams rather than hex encoding when printing BLOB
  181. results. <em>Since Ant 1.7.1</em>.</td>
  182. <td>No; defaults to <q>false</q></td>
  183. </tr>
  184. <tr>
  185. <td>failOnConnectionError</td>
  186. <td>If <q>false</q>, will only print a warning message and not execute any statement if the task
  187. fails to connect to the database. <em>Since Ant 1.8.0</em>.</td>
  188. <td>No; defaults to <q>true</q></td>
  189. </tr>
  190. <tr>
  191. <td>strictDelimiterMatching</td>
  192. <td>If <q>false</q>, delimiters will be searched for in a case-insensitive manner
  193. (i.e. <var>delimiter</var>=<q>go</q> matches <q>GO</q>) and surrounding whitespace will be
  194. ignored (<var>delimiter</var>=<q>go</q> matches <q>GO </q>). <em>Since Ant 1.8.0</em>.</td>
  195. <td>No; defaults to <q>true</q></td>
  196. </tr>
  197. <tr>
  198. <td>showWarnings</td>
  199. <td>If <q>true</q>, SQLWarnings will be logged at the <q>warning</q> level. <em>Since Ant
  200. 1.8.0</em>.<br/><strong>Note</strong>: even if the attribute is set to <q>false</q>, warnings
  201. that apply to the connection will be logged at the <q>verbose</q> level.</td>
  202. <td>No; defaults to <q>false</q></td>
  203. </tr>
  204. <tr>
  205. <td>treatWarningsAsErrors</td>
  206. <td>If <q>true</q>, SQLWarnings will be treated like errors&mdash;and the logic selected via
  207. the <var>onError</var> attribute applies. <em>Since Ant 1.8.0</em>.</td>
  208. <td>No; defaults to <q>false</q></td>
  209. </tr>
  210. <tr>
  211. <td>csvColumnSeparator</td>
  212. <td>The column separator used when printing the results. <em>Since Ant 1.8.0</em>.</td>
  213. <td>No; defaults to <q>,</q></td>
  214. </tr>
  215. <tr>
  216. <td>csvQuoteCharacter</td>
  217. <td>The character used to quote column values.<br/>If set, columns that contain either the
  218. column separator or the quote character itself will be surrounded by the quote character. The
  219. quote character itself will be doubled if it appears inside of the column's
  220. value.<br/><strong>Note</strong>: BLOB values will never be quoted. <em>Since Ant
  221. 1.8.0</em>.</td>
  222. <td>No; default is not set (i.e. no quoting ever occurs)</td>
  223. </tr>
  224. <tr>
  225. <td>forceCsvQuoteChar</td>
  226. <td>If <q>true</q>, quoting always occurs</td>
  227. <td>No; default is not set (i.e. quoting occurs only where needed)</td>
  228. </tr>
  229. <tr>
  230. <td>errorproperty</td>
  231. <td>The name of a property to set in the event of an error. <em>Since Ant 1.8.0</em></td>
  232. <td>No</td>
  233. </tr>
  234. <tr>
  235. <td>warningproperty</td>
  236. <td>The name of a property to set in the event of an warning. <em>Since Ant 1.8.0</em></td>
  237. <td>No</td>
  238. </tr>
  239. <tr>
  240. <td>rowcountproperty</td>
  241. <td>The name of a property to set to the number of rows updated by the first
  242. statement/transaction that actually returned a row count. <em>Since Ant 1.8.0</em></td>
  243. <td>No</td>
  244. </tr>
  245. </table>
  246. <h3>Parameters specified as nested elements</h3>
  247. <h4>transaction</h4>
  248. <p>Use nested <code>&lt;transaction&gt;</code> elements to specify multiple blocks of commands to
  249. the executed executed in the same connection but different transactions. This is particularly useful
  250. when there are multiple files to execute on the same schema.</p>
  251. <table class="attr">
  252. <tr>
  253. <th scope="col">Attribute</th>
  254. <th scope="col">Description</th>
  255. <th scope="col">Required</th>
  256. </tr>
  257. <tr>
  258. <td>src</td>
  259. <td>File containing SQL statements</td>
  260. <td>Yes, unless statements enclosed within tags</td>
  261. </tr>
  262. </table>
  263. <p>The <code>&lt;transaction&gt;</code> element supports
  264. any <a href="../Types/resources.html">resource</a> or single element resource collection as nested
  265. element to specify the resource containing the SQL statements.</p>
  266. <h4>any <a href="../Types/resources.html">resource</a> or resource collection</h4>
  267. <p>You can specify multiple sources via nested resource collection elements. Each resource of the
  268. collection will be run in a transaction of its own. Prior to Ant 1.7 only filesets were supported.
  269. Use a sort resource collection to get a predictable order of transactions.</p>
  270. <h4>classpath</h4>
  271. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a href="../using.html#path">path-like
  272. structure</a> and can also be set via a nested <em>classpath</em> element. It is used to load the
  273. JDBC classes.</p>
  274. <h4>connectionProperty</h4>
  275. <p><em>Since Ant 1.8.0</em></p>
  276. <p>Use nested <code>&lt;connectionProperty&gt;</code> elements to specify additional JDBC properties
  277. that need to be set when connecting to the database.</p>
  278. <table class="attr">
  279. <tr>
  280. <th scope="col">Attribute</th>
  281. <th scope="col">Description</th>
  282. <th scope="col">Required</th>
  283. </tr>
  284. <tr>
  285. <td>name</td>
  286. <td>Name of the property</td>
  287. <td>Yes</td>
  288. </tr>
  289. <tr>
  290. <td>value</td>
  291. <td>Value of the property</td>
  292. <td>Yes</td>
  293. </tr>
  294. </table>
  295. <h3>Examples</h3>
  296. <p>Connect to the database given in <var>url</var> as the <q>sa</q> user using
  297. the <code>org.database.jdbcDriver</code> and execute the SQL statements contained within the
  298. file <samp>data.sql</samp></p>
  299. <pre>
  300. &lt;sql 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;/&gt;</pre>
  305. <p>Connect to the database given in <var>url</var> as the <q>sa</q> user using
  306. the <code>org.database.jdbcDriver</code> and execute the SQL statements contained within the
  307. file <samp>data.sql</samp>. Also set the property <code>internal_logon</code> to the
  308. value <q>SYSDBA</q>.</p>
  309. <pre>
  310. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  311. url=&quot;jdbc:database-url&quot;
  312. userid=&quot;sa&quot;
  313. password=&quot;pass&quot;
  314. src=&quot;data.sql&quot;&gt;
  315. &lt;connectionProperty name=&quot;internal_logon&quot; value=&quot;SYSDBA&quot;&gt;
  316. &lt;/sql&gt;</pre>
  317. <p>Connect to the database given in <var>url</var> as the <q>sa</q> user using
  318. the <code>org.database.jdbcDriver</code> and execute the two SQL statements inserting data
  319. into <samp>some_table</samp> and truncating <samp>some_other_table</samp>. Ant properties in the
  320. nested text will not be expanded.</p>
  321. <pre>
  322. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  323. url=&quot;jdbc:database-url&quot;
  324. userid=&quot;sa&quot;
  325. password=&quot;pass&quot;&gt;
  326. insert
  327. into table some_table
  328. values(1,2,3,4);
  329. truncate table some_other_table;
  330. &lt;/sql&gt;</pre>
  331. <p>Note that you may want to enclose your statements in <code>&lt;![CDATA[</code>
  332. ... <code>]]&gt;</code> sections so you don't need to
  333. escape <code>&lt;</code>, <code>&gt;</code>, <code>&amp;</code> or other special characters. For
  334. example:</p>
  335. <pre>
  336. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  337. url=&quot;jdbc:database-url&quot;
  338. userid=&quot;sa&quot;
  339. password=&quot;pass&quot;&gt;&lt;![CDATA[
  340. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  341. ]]&gt;&lt;/sql&gt;</pre>
  342. <p>The following command turns property expansion in nested text on (it is off purely for backwards
  343. compatibility), then creates a new user in the HSQLDB database using Ant properties.</p>
  344. <pre>
  345. &lt;sql driver="org.hsqldb.jdbcDriver";
  346. url="jdbc:hsqldb:file:${database.dir}"
  347. userid="sa"
  348. password=""
  349. expandProperties="true"&gt;
  350. &lt;transaction&gt;
  351. CREATE USER ${newuser} PASSWORD ${newpassword}
  352. &lt;/transaction&gt;
  353. &lt;/sql&gt;</pre>
  354. <p>The following connects to the database given in <var>url</var> as the <q>sa</q> user using
  355. the <code>org.database.jdbcDriver</code> and executes the SQL statements contained within the
  356. files <samp>data1.sql</samp>, <samp>data2.sql</samp> and <samp>data3.sql</samp> and then executes
  357. the truncate operation on <samp>some_other_table</samp>.</p>
  358. <pre>
  359. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  360. url=&quot;jdbc:database-url&quot;
  361. userid=&quot;sa&quot;
  362. password=&quot;pass&quot;&gt;
  363. &lt;transaction src=&quot;data1.sql&quot;/&gt;
  364. &lt;transaction src=&quot;data2.sql&quot;/&gt;
  365. &lt;transaction src=&quot;data3.sql&quot;/&gt;
  366. &lt;transaction&gt;
  367. truncate table some_other_table;
  368. &lt;/transaction&gt;
  369. &lt;/sql&gt;</pre>
  370. <p>The following example does the same as (and may execute additional SQL files if there are more
  371. files matching the pattern <samp>data*.sql</samp>) but doesn't guarantee that <samp>data1.sql</samp>
  372. will be run before <samp>data2.sql</samp>.</p>
  373. <pre>
  374. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  375. url=&quot;jdbc:database-url&quot;
  376. userid=&quot;sa&quot;
  377. password=&quot;pass&quot;&gt;
  378. &lt;path&gt;
  379. &lt;fileset dir=&quot;.&quot;&gt;
  380. &lt;include name=&quot;data*.sql&quot;/&gt;
  381. &lt;/fileset&gt;
  382. &lt;/path&gt;
  383. &lt;transaction&gt;
  384. truncate table some_other_table;
  385. &lt;/transaction&gt;
  386. &lt;/sql&gt;</pre>
  387. <p>The following connects to the database given in <var>url</var> as the <q>sa</q> user using
  388. the <code>org.database.jdbcDriver</code> and executes the SQL statements contained within the
  389. file <samp>data.sql</samp>, with output piped to <samp>outputfile.txt</samp>,
  390. searching <samp>/some/jdbc.jar</samp> as well as the system classpath for the driver class.</p>
  391. <pre>
  392. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  393. url=&quot;jdbc:database-url&quot;
  394. userid=&quot;sa&quot;
  395. password=&quot;pass&quot;
  396. src=&quot;data.sql&quot;
  397. print=&quot;yes&quot;
  398. output=&quot;outputfile.txt&quot;&gt;
  399. &lt;classpath&gt;
  400. &lt;pathelement location=&quot;/some/jdbc.jar&quot;/&gt;
  401. &lt;/classpath&gt;
  402. &lt;/sql&gt;</pre>
  403. <p>The following will only execute if the RDBMS is <q>oracle</q> and the version starts
  404. with <q>8.1.</q></p>
  405. <pre>
  406. &lt;sql driver=&quot;org.database.jdbcDriver&quot;
  407. url=&quot;jdbc:database-url&quot;
  408. userid=&quot;sa&quot;
  409. password=&quot;pass&quot;
  410. src=&quot;data.sql&quot;
  411. rdbms=&quot;oracle&quot;
  412. version=&quot;8.1.&quot;&gt;
  413. insert
  414. into table some_table
  415. values(1,2,3,4);
  416. truncate table some_other_table;
  417. &lt;/sql&gt;</pre>
  418. </body>
  419. </html>