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

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