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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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">output</td>
  84. <td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
  85. <td width="10%" valign="top">No (print to System.out by default)</td>
  86. </tr>
  87. <tr>
  88. <td valign="top">append</td>
  89. <td valign="top">whether output should be appended to or overwrite
  90. an existing file. Defaults to false.</td>
  91. <td align="center" valign="top">No</td>
  92. </tr>
  93. <tr>
  94. <td width="12%" valign="top">classpath</td>
  95. <td width="78%" valign="top">Classpath used to load driver</td>
  96. <td width="10%" valign="top">No (use system classpath)</td>
  97. </tr>
  98. <tr>
  99. <td width="12%" valign="top">classpathref</td>
  100. <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>
  101. <td width="10%" valign="top">No (use system classpath)</td>
  102. </tr>
  103. <tr>
  104. <td width="12%" valign="top">onerror</td>
  105. <td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
  106. <td width="10%" valign="top">No, default &quot;abort&quot;</td>
  107. </tr>
  108. <tr>
  109. <td width="12%" valign="top">rdbms</td>
  110. <td width="78%" valign="top">Execute task only if this rdbms</td>
  111. <td width="10%" valign="top">No (no restriction)</td>
  112. </tr>
  113. <tr>
  114. <td width="12%" valign="top">version</td>
  115. <td width="78%" valign="top">Execute task only if rdbms version match</td>
  116. <td width="10%" valign="top">No (no restriction)</td>
  117. </tr>
  118. <tr>
  119. <td width="12%" valign="top">caching</td>
  120. <td width="78%" valign="top">Should the task cache loaders and the driver?</td>
  121. <td width="10%" valign="top">No (default=true)</td>
  122. </tr>
  123. <tr>
  124. <td width="12%" valign="top">delimitertype</td>
  125. <td width="78%" valign="top">Control whether the delimiter will only be recognized on a line by itself.<br>
  126. Can be "normal" -anywhere on the line, or "row", meaning it must be on a line by itself</td>
  127. <td width="10%" valign="top">No (defaul:normal)</td>
  128. </tr>
  129. <tr>
  130. <td width="12%" valign="top">keepformat</td>
  131. <td width="78%" valign="top">Control whether the format of the sql will be preserved.<br>
  132. Usefull when loading packages and procedures.
  133. <td width="10%" valign="top">No (defaul=false)</td>
  134. </tr>
  135. <tr>
  136. <td width="12%" valign="top">escapeprocessing</td>
  137. <td width="78%" valign="top">Control whether the Java statement
  138. object will perform escape substitution.<br>
  139. See <a
  140. href="http://java.sun.com/j2se/1.4.2/docs/api/java/sql/Statement.html#setEscapeProcessing(boolean)">Statement's
  141. API docs</a> for details. <em>since Ant 1.6</em>.
  142. <td width="10%" valign="top">No (defaul=true)</td>
  143. </tr>
  144. </table>
  145. <h3>Parameters specified as nested elements</h3>
  146. <h4>transaction</h4>
  147. <p>Use nested <code>&lt;transaction&gt;</code>
  148. elements to specify multiple blocks of commands to the executed
  149. executed in the same connection but different transactions. This
  150. is particularly useful when there are multiple files to execute
  151. on the same schema.</p>
  152. <table border="1" cellpadding="2" cellspacing="0">
  153. <tr>
  154. <td valign="top"><b>Attribute</b></td>
  155. <td valign="top"><b>Description</b></td>
  156. <td align="center" valign="top"><b>Required</b></td>
  157. </tr>
  158. <tr>
  159. <td valign="top">src</td>
  160. <td valign="top">File containing SQL statements</td>
  161. <td valign="top" align="center">Yes, unless statements enclosed within tags</td>
  162. </tr>
  163. </table>
  164. <h4>fileset</h4>
  165. <p>You can specify multiple source files via nested <a
  166. href="../CoreTypes/fileset.html">fileset</a> elements. Each file of
  167. the fileset will be run in a transaction of its own, the order by
  168. which the files of a single fileset will be executed is not
  169. defined.</p>
  170. <h4>classpath</h4>
  171. <p><code>Sql</code>'s <em>classpath</em> attribute is a <a
  172. href="../using.html#path">PATH like structure</a> and can also be set via a nested
  173. <em>classpath</em> element. It is used to load the JDBC classes.</p>
  174. <h3>Examples</h3>
  175. <blockquote><pre>&lt;sql
  176. driver=&quot;org.database.jdbcDriver&quot;
  177. url=&quot;jdbc:database-url&quot;
  178. userid=&quot;sa&quot;
  179. password=&quot;pass&quot;
  180. src=&quot;data.sql&quot;
  181. /&gt;
  182. </pre></blockquote>
  183. <p>Connects to the database given in <i>url</i> as the sa user using the
  184. org.database.jdbcDriver and executes the SQL statements contained within
  185. the file data.sql</p>
  186. <blockquote><pre>&lt;sql
  187. driver=&quot;org.database.jdbcDriver&quot;
  188. url=&quot;jdbc:database-url&quot;
  189. userid=&quot;sa&quot;
  190. password=&quot;pass&quot;
  191. &gt;
  192. insert
  193. into table some_table
  194. values(1,2,3,4);
  195. truncate table some_other_table;
  196. &lt;/sql&gt;
  197. </pre></blockquote>
  198. <p>Connects to the database given in <i>url</i> as the sa
  199. user using the org.database.jdbcDriver and executes the two SQL statements
  200. inserting data into some_table and truncating some_other_table </p>
  201. <p>Note that you may want to enclose your statements in
  202. <code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
  203. need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
  204. or other special characters. For example:</p>
  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. &gt;&lt;![CDATA[
  211. update some_table set column1 = column1 + 1 where column2 &lt; 42;
  212. ]]&gt;&lt;/sql&gt;
  213. </pre></blockquote>
  214. <p>The following connects to the database given in url as the sa user using
  215. the org.database.jdbcDriver and executes the SQL statements contained within
  216. the files data1.sql, data2.sql and data3.sql and then executes the truncate
  217. operation on <i>some_other_table</i>.</p>
  218. <blockquote><pre>&lt;sql
  219. driver=&quot;org.database.jdbcDriver&quot;
  220. url=&quot;jdbc:database-url&quot;
  221. userid=&quot;sa&quot;
  222. password=&quot;pass&quot; &gt;
  223. &lt;transaction src=&quot;data1.sql&quot;/&gt;
  224. &lt;transaction src=&quot;data2.sql&quot;/&gt;
  225. &lt;transaction src=&quot;data3.sql&quot;/&gt;
  226. &lt;transaction&gt;
  227. truncate table some_other_table;
  228. &lt;/transaction&gt;
  229. &lt;/sql&gt;
  230. </pre></blockquote>
  231. <p>The following example does the same as (and may execute additional
  232. SQL files if there are more files matching the pattern
  233. <code>data*.sql</code>) but doesn't guarantee that data1.sql will be
  234. run before <code>data2.sql</code>.</p>
  235. <blockquote><pre>&lt;sql
  236. driver=&quot;org.database.jdbcDriver&quot;
  237. url=&quot;jdbc:database-url&quot;
  238. userid=&quot;sa&quot;
  239. password=&quot;pass&quot;&gt;
  240. &lt;fileset dir=&quot;.&quot;&gt;
  241. &lt;include name=&quot;data*.sql&quot;/&gt;
  242. &lt;/fileset&gt;
  243. &lt;transaction&gt;
  244. truncate table some_other_table;
  245. &lt;/transaction&gt;
  246. &lt;/sql&gt;
  247. </pre></blockquote>
  248. <p>The following connects to the database given in url as the sa user using the
  249. org.database.jdbcDriver and executes the SQL statements contained within the
  250. file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar
  251. as well as the system classpath for the driver class.</p>
  252. <blockquote><pre>&lt;sql
  253. driver=&quot;org.database.jdbcDriver&quot;
  254. url=&quot;jdbc:database-url&quot;
  255. userid=&quot;sa&quot;
  256. password=&quot;pass&quot;
  257. src=&quot;data.sql&quot;
  258. print=&quot;yes&quot;
  259. output=&quot;outputfile.txt&quot;
  260. &gt;
  261. &lt;classpath&gt;
  262. &lt;pathelement location=&quot;/some/jdbc.jar&quot;/&gt;
  263. &lt;/classpath&gt;
  264. &lt;/sql&gt;
  265. </pre></blockquote>
  266. <p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
  267. starts with &quot;8.1.&quot;</p>
  268. <blockquote><pre>&lt;sql
  269. driver=&quot;org.database.jdbcDriver&quot;
  270. url=&quot;jdbc:database-url&quot;
  271. userid=&quot;sa&quot;
  272. password=&quot;pass&quot;
  273. src=&quot;data.sql&quot;
  274. rdbms=&quot;oracle&quot;
  275. version=&quot;8.1.&quot;
  276. &gt;
  277. insert
  278. into table some_table
  279. values(1,2,3,4);
  280. truncate table some_other_table;
  281. &lt;/sql&gt;
  282. </pre></blockquote>
  283. <hr>
  284. <p align="center">Copyright &copy; 2000-2005 The Apache Software Foundation. All rights
  285. Reserved.</p>
  286. </body>
  287. </html>