blob: 579bc3888a79951a2c19343c7bd3033b7af155a0 [file] [log] [blame]
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<title>Ant User Manual</title>
</head>
<body>
<h2><a name="sql">Sql</a></h2>
<h3>Description</h3>
<p>Executes a series of sql statement via JDBC to a database. Statements can either be read in from a text file using the src attribute or from between the enclosing sql tags.</p>
<p>Multiple statements can be set and each statement is delimited from the next use a semi-colon. Individual lines within the statements can be commented using either -- or // at the start of the line.</p>
<p>The auto-commit attribute specifies whether auto commit should be turned on or off whilst executing the statements. If auto-commit is turned on each statement will be executed and committed. If it is turned off the statements will all be executed as one transaction.</p>
<p>The <i>onerror</i> attribute specifies how to proceed when an error occurs during the execution of one of the statements.
The possible values are: <b>continue</b> execution, only show the error;
<b>stop</b> execution and commit transaction;
and <b>abort</b> execution and transaction and fail task.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td width="12%" valign="top"><b>Attribute</b></td>
<td width="78%" valign="top"><b>Description</b></td>
<td width="10%" valign="top"><b>Required</b></td>
</tr>
<tr>
<td width="12%" valign="top">driver</td>
<td width="78%" valign="top">Class name of the jdbc driver</td>
<td width="10%" valign="top">Yes</td>
</tr>
<tr>
<td width="12%" valign="top">url</td>
<td width="78%" valign="top">Database connection url</td>
<td width="10%" valign="top">Yes</td>
</tr>
<tr>
<td width="12%" valign="top">userid</td>
<td width="78%" valign="top">Database user name</td>
<td width="10%" valign="top">Yes</td>
</tr>
<tr>
<td width="12%" valign="top">password</td>
<td width="78%" valign="top">Database password</td>
<td width="10%" valign="top">Yes</td>
</tr>
<tr>
<td width="12%" valign="top">src</td>
<td width="78%" valign="top">File containing sql statements</td>
<td width="10%" valign="top">Yes, unless statements enclosed within tags</td>
</tr>
<tr>
<td width="12%" valign="top">autocommit</td>
<td width="78%" valign="top">Auto commit flag for database connection (default false)</td>
<td width="10%" valign="top">No, default &quot;false&quot;</td>
</tr>
<tr>
<td width="12%" valign="top">print</td>
<td width="78%" valign="top">Print result sets from the statements (default false)</td>
<td width="10%" valign="top">No, default &quot;false&quot;</td>
</tr>
<tr>
<td width="12%" valign="top">showheaders</td>
<td width="78%" valign="top">Print headers for result sets from the statements (default true)</td>
<td width="10%" valign="top">No, default &quot;true&quot;</td>
</tr>
<tr>
<td width="12%" valign="top">output</td>
<td width="78%" valign="top">Output file for result sets (defaults to System.out)</td>
<td width="10%" valign="top">No (print to System.out by default)</td>
</tr>
<tr>
<td width="12%" valign="top">classpath</td>
<td width="78%" valign="top">Classpath used to load driver</td>
<td width="10%" valign="top">No (use system classpath)</td>
</tr>
<tr>
<td width="12%" valign="top">onerror</td>
<td width="78%" valign="top">Action to perform when statement fails: continue, stop, abort</td>
<td width="10%" valign="top">No, default &quot;abort&quot;</td>
</tr>
<tr>
<td width="12%" valign="top">rdbms</td>
<td width="78%" valign="top">Execute task only if this rdbms</td>
<td width="10%" valign="top">No (no restriction)</td>
</tr>
<tr>
<td width="12%" valign="top">version</td>
<td width="78%" valign="top">Execute task only if rdbms version match</td>
<td width="10%" valign="top">No (no restriction)</td>
</tr>
</table>
<h3>Parameters specified as nested elements</h3>
<h4>transaction</h4>
<p>Use nested <code>&lt;transaction&gt;</code>
elements to specify multiple blocks of commands to the executed
executed in the same connection but different transactions. This
is particularly useful when there are multiple files to execute
on the same schema.</p>
<table border="1" cellpadding="2" cellspacing="0">
<tr>
<td valign="top"><b>Attribute</b></td>
<td valign="top"><b>Description</b></td>
<td align="center" valign="top"><b>Required</b></td>
</tr>
<tr>
<td valign="top">src</td>
<td valign="top">File containing sql statements</td>
<td valign="top" align="center">Yes, unless statements enclosed within tags</td>
</tr>
</table>
<h4>classpath</h4>
<p><code>Sql</code>'s <em>classpath</em> attribute is a <a
href="../using.html#path">PATH like structure</a> and can also be set via a nested
<em>classpath</em> element. It is used to load the JDBC classes.</p>
<p>
The
</p>
<h3>Examples</h3>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot;
src=&quot;data.sql&quot;
/&gt;
</pre></blockquote>
<p>Connects to the database given in url as the sa user using the org.database.jdbcDriver and executes the sql statements contained within the file data.sql</p>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot;
&gt;
insert
into table some_table
values(1,2,3,4);
truncate table some_other_table;
&lt;/sql&gt;
</pre></blockquote>
<p>Connects to the database given in url as the sa user using the org.database.jdbcDriver and executes the two sql statements inserting data into some_table and truncating some_other_table</p>
<p>Note that you may want to enclose your statements in
<code>&lt;![CDATA[</code> ... <code>]]&gt;</code> sections so you don't
need to escape <code>&lt;</code>, <code>&gt;</code> <code>&amp;</code>
or other special characters. For example:</p>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot;
&gt;&lt;![CDATA[
update some_table set column1 = column1 + 1 where column2 &lt; 42;
]]&gt;&lt;/sql&gt;
</pre></blockquote>
<p>The following connects to the database given in url as the sa user using the org.database.jdbcDriver and executes the sql statements contained within the files data1.sql, data2.sql and data3.sql and then executes the truncate operation on <i>some_other_table</i>.</p>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot; &gt;
&lt;transaction src=&quot;data1.sql&quot; /&gt;
&lt;transaction src=&quot;data2.sql&quot; /&gt;
&lt;transaction src=&quot;data3.sql&quot; /&gt;
&lt;transaction&gt;
truncate table some_other_table;
&lt;/transaction&gt;
&lt;/sql&gt;
</pre></blockquote>
<p>The following connects to the database given in url as the sa user using the org.database.jdbcDriver and executes the sql statements contained within the file data.sql, with output piped to outputfile.txt, searching /some/jdbc.jar as well as the system classpath for the driver class.</p>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot;
src=&quot;data.sql&quot;
print=&quot;yes&quot;
output=&quot;outputfile.txt&quot;
&gt;
&lt;classpath&gt;
&lt;pathelement location=&quot;/some/jdbc.jar&quot;&gt;
&lt;/classpath&gt;
&lt;/sql&gt;
</pre></blockquote>
<p>The following will only execute if the RDBMS is &quot;oracle&quot; and the version
starts with &quot;8.1.&quot;</p>
<blockquote><pre>&lt;sql
driver=&quot;org.database.jdbcDriver&quot;
url=&quot;jdbc:database-url&quot;
userid=&quot;sa&quot;
password=&quot;pass&quot;
src=&quot;data.sql&quot;
rdbms=&quot;oracle&quot;
version=&quot;8.1.&quot;
&gt;
insert
into table some_table
values(1,2,3,4);
truncate table some_other_table;
&lt;/sql&gt;
</pre></blockquote>
</body>
</html>