在从ant sql任务调用时,如何将参数传递给PL / SQL过程?

时间:2015-06-26 07:06:12

标签: sql oracle ant plsql

我有一个蚂蚁目标,如 -

<target name="run-patched-sql-file">
    <path id="antclasspath">
        <fileset dir="${weblogic.server.dir}/server/lib">
            <include name="*ojdbc6.jar"/>
        </fileset>
    </path>
    <property name="pathvar" refid="antclasspath"/>
    <filelist id="sql-files" dir="../../db/scripts/oracle/">
            <file name="scripts/scriptToExecute.sql"/>
    </filelist>
    <sql driver="${operationsDB.driver}" url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=${operationsDB.host}) (PORT=${operationsDB.port}))(CONNECT_DATA=(SERVICE_NAME=${operationsDB.serviceName})))" userid="${operationsDB.user}" password="${OIM.DBPassword}" delimiter="/" delimitertype="row" keepformat="yes" onerror="continue" caching="true" escapeprocessing="no" classpathref="antclasspath">
        <path>
            <filelist refid="sql-files"/>
        </path>
    </sql>
</target>

现在scriptToExecute.sql需要一个参数。 我们如何将这个参数从ant任务传递给sql脚本。

4 个答案:

答案 0 :(得分:0)

DECLARE DC_NAME VARCHAR2(100) := '&1'; 
   BEGIN 
   INSERT INTO PTY (PTY_KEYWORD,PTY_VALUE,PTY_UPDATE,PTY_UPDATEBY) 
    VALUES ('DCNamepty',DC_NAME,SYSDATE,'ADMIN'); 
    COMMIT; 
    END; 

这意味着,当您执行此.sql文件时。它会提示输入值。正如我所看到的,变量DC_NAME的变量替换用作&1

答案 1 :(得分:0)

您可以使用模板和一些copy任务来执行replacetokens替换:

<copy todir="." overwrite="true" verbose="true">
 <fileset dir="." includes="scriptToExecute.tpl"/>
 <mapper type="glob" from="*.tpl" to="*.sql"/>
 <filterchain>
  <replacetokens>
    <token key="DC_NAME" value="the name of my DC"/>
  </replacetokens>
 </filterchain>
</copy>

以下scriptToExecute.tpl请注意@DC_NAME@模板令牌:

INSERT INTO PTY (PTY_KEYWORD,PTY_VALUE,PTY_UPDATE,PTY_UPDATEBY) 
VALUES ('DCNamepty','@DC_NAME@',SYSDATE,'ADMIN'); 
COMMIT;

另一种方法是使用FilterChain添加ExpandProperties以实现类似的目标。然后,查询模板将使用参数扩展:${DC_NAME}

答案 2 :(得分:0)

您是否尝试过直接运行sqlplus?

<exec executable="sqlplus" 
      dir="./" output="${logfile}" failOnError="true">
  <arg value="${db_user}/${db_passwd}@${tnsalias_or_tnsurl}" />
  <arg value="@scripts/scriptToExecute.sql"/> 
  <arg value="the_value_for_param_1"/> 
</exec>

答案 3 :(得分:0)

达到了

<target name="run-patched-sql-file">
    <path id="antclasspath">
        <fileset dir="${weblogic.server.dir}/server/lib">
            <include name="*ojdbc6.jar"/>
        </fileset>
    </path>
    <property name="pathvar" refid="antclasspath"/>
    <filelist id="sql-files" dir="../../db/scripts/oracle/">
            <file name="scripts/scriptToExecute.sql"/>
    </filelist>
    <sql driver="${operationsDB.driver}" url="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=${operationsDB.host}) (PORT=${operationsDB.port}))(CONNECT_DATA=(SERVICE_NAME=${operationsDB.serviceName})))" userid="${operationsDB.user}" password="${operationsDB.DBPassword}" delimiter="/" delimitertype="row" keepformat="yes" onerror="continue" caching="true" escapeprocessing="no" classpathref="antclasspath">
        <![CDATA[
            DECLARE 
            DC_NAME VARCHAR2(100) := '${DataCenter.NAME}'; 
            BEGIN 
                    INSERT INTO pty (pty_keyword,pty_value,pty_update,pty_updateby) 
                        VALUES ('DCNamepty',DC_NAME,SYSDATE,'ADMIN'); 
            COMMIT; 
            END; 
        ]]>
    </sql>
</target>