简体   繁体   English

使用Apache Ant而不是Oracle SQL Developer执行SQL时出错

[英]Error executing SQL with Apache Ant, but not Oracle SQL Developer

Following is a trigger: 以下是触发器:

CREATE OR REPLACE TRIGGER "CMDC"."USER_ROADS_UC"
BEFORE INSERT OR UPDATE OF
    ASSOCIATED_PARENT_ROAD
ON USER_ROADS
REFERENCES NEW AS NEW
FOR EACH ROW BEGIN
    :new.ASSOCIATED_PARENT_ROAD:=upper(:new.ASSOCIATED_PARENT_ROAD);
    EXCEPTION
    WHEN OTHERS THEN RETURN;
END;
/
ALTER TRIGGER "CMDC"."USER_ROADS_UC" ENABLE

The above runs perfectly in SQL Developer. 上面的代码可以在SQL Developer中完美运行。 However, when I get the following error when I run it using Ant: 但是,当我使用Ant运行它时出现以下错误:

[sql] Failed to execute:  EXCEPTION WHEN OTHERS THEN RETURN
[sql] java.sql.SQLException: ORA-00900: invalid SQL statement
[sql] Failed to execute:  END
[sql] java.sql.SQLException: ORA-00900: invalid SQL statement
[sql] Failed to execute:  / ALTER TRIGGER "CMDC"."USER_ROADS_UC" ENABLE
[sql] java.sql.SQLException: ORA-00900: invalid SQL statement

I have already reviewed this question . 我已经审查了这个问题 However, I still couldn't modify the script to work with Ant. 但是,我仍然无法修改脚本以与Ant一起使用。

Following is the ant target 以下是蚂蚁的目标

<target name="create-db-schema" >
        <echo message="############################################################"/>
        <echo message="#               Create Complete DB Schema                  #"/>
        <echo message="############################################################"/>

        <sql onerror="continue" classpathref="project.class.path" driver="${database.driverClassName}"
             url="${database.url}" userid="${database.username}" password="${database.password}">
            <path>
                <fileset dir="${test.dbscripts.dir}/schema/">
                    <include name="*.sql"/>
                </fileset>
            </path>
        </sql>
</target>

With ant sql tag I couldn't run both SQL and PL/SQL in the same script even though I tried in many ways. 使用ant sql标记,即使我尝试了很多方法,我也无法在同一脚本中同时运行SQL和PL / SQL。

But using an external library I could do this.I've used this library to do that dbmaintain Add following to your build script. 但是我可以使用外部库来做到这一点。我已经使用该库来执行dbmaintain添加到您的构建脚本中。

<path id="dbmaintain-lib"><fileset dir="${dbmaintain.home}/lib"><include name="*.jar"/></fileset></path>
<taskdef resource="dbmaintain-anttasks.xml" classpathref="dbmaintain.lib"/>

    <target name="update-db">
    <updateDatabase scriptLocations="scripts" autoCreateDbMaintainScriptsTable="true">
        <database driverClassName="oracle.jdbc.driver.OracleDriver" userName="user" password="pass" url="jdbc:oracle:thin:@//localhost:1521/XE" schemaNames="SCHEMA"/>
    </updateDatabase>
</target>

And everthing works perfectly now without changes to sql scripts files to replace ';'. 现在一切都可以完美运行,而无需更改sql脚本文件来替换';'。 Hope this'll help for somebody in need. 希望这对有需要的人有所帮助。

I don't have environment to test this solution. 我没有环境可以测试此解决方案。 But for testing purpose you can try something like this. 但是出于测试目的,您可以尝试这样的操作。

<sql
    driver="xxx"
    url="xxx"
    userid="xxx"
    password="xxx"
    delimiter="/"
    delimitertype="row"
    ><![CDATA[

CREATE OR REPLACE TRIGGER "CMDC"."USER_ROADS_UC"
BEFORE INSERT OR UPDATE OF
    ASSOCIATED_PARENT_ROAD
ON USER_ROADS
REFERENCES NEW AS NEW
FOR EACH ROW BEGIN
    :new.ASSOCIATED_PARENT_ROAD:=upper(:new.ASSOCIATED_PARENT_ROAD);
    EXCEPTION
    WHEN OTHERS THEN RETURN;
END;
/
ALTER TRIGGER "CMDC"."USER_ROADS_UC" ENABLE
/

]]></sql>
  • Important ! 重要! Your delimiter now is "/" in new line without any surrounding whitespaces. 现在,您的定界符在换行符中为“ /”,周围没有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM