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

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