ANT sql 任务:如何运行 SQL 和 PL/SQL 并注意执行失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3363356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:00:36  来源:igfitidea点击:

ANT sql task: How to run SQL and PL/SQL and notice execution failure?

sqlantplsql

提问by Peter

to execute an .sql script file from ANT it works fine using the following task:

要从 ANT 执行 .sql 脚本文件,它可以使用以下任务正常工作:

<sql
    classpath="${oracle.jar}" driver="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@@@{db.hostname}:@{db.port}:@{db.sid}" 
    userid="@{db.user}" 
    password="@{db.password}"
    src="@{db.sql.script}" />

But if the .sql file not only contains pure SQL but also PL/SQL the task will fail. This could be solved by using the following snippet:

但是,如果 .sql 文件不仅包含纯 SQL,还包含 PL/SQL,则任务将失败。这可以通过使用以下代码段来解决:

<sql
    classpath="${oracle.jar}" driver="oracle.jdbc.OracleDriver"
    url="jdbc:oracle:thin:@@@{db.hostname}:@{db.port}:@{db.sid}" 
    userid="@{db.user}" 
    password="@{db.password}"
    delimiter="/"
    delimitertype="row"
    src="@{db.sql.script}" />

But if my script contains both SQL andPL/SQL then neither ANT task will work. Another solution would be to use the "exec" task with "sqlplus":

但是如果我的脚本同时包含 SQLPL/SQL,那么 ANT 任务都不会工作。另一种解决方案是将“exec”任务与“sqlplus”一起使用:

<exec executable="sqlplus" failonerror="true" errorproperty="exit.status">
    <arg value="${db.user}/${db.password}@${db.hostname}:${db.port}/${db.sid}"/>
    <arg value="@${db.sql.script}"/>
</exec>

But unfortunately this task will never fail, hence the build returns always with "SUCCESSFUL" even though the sql script execution failed. The error property which I tried to set would not return any error code.

但不幸的是,这个任务永远不会失败,因此即使 sql 脚本执行失败,构建也总是返回“成功”。我尝试设置的错误属性不会返回任何错误代码。

Any ideas/suggestions how to solve this problem?

任何想法/建议如何解决这个问题?

Thanks,

谢谢,

Peter

彼得

回答by Michael Pakhantsov

Peter,

彼得,

Add at the beginning of scripts

在脚本开头添加

  WHENEVER SQLERROR EXIT SQL.CODE;

Then sqlplus will exit with exit code != 0.

然后 sqlplus 将以退出代码 != 0 退出。

回答by user967710

Pretty late, I guess - but I hope this will help someone:

我想很晚了 - 但我希望这会帮助某人:

In general, I think we should perfer using sql rather than exec executable="sqlplus" for many reasons, such as: in case we change DB provider, you don't spend reaources in a new process with sql, "STOPPING" will work as opposed to sqlplus.exe etc.

总的来说,我认为我们应该优先使用 sql 而不是 exec executable="sqlplus" ,原因有很多,例如:如果我们更改数据库提供程序,您不会在使用 sql 的新进程中花费资源,“STOPPING”将起作用与 sqlplus.exe 等相反。

Anyway, here's a suggestion on how to let both PL/SQL & SQL in same script so that it will work:

无论如何,这里有一个关于如何在同一个脚本中同时使用 PL/SQL 和 SQL 以使其工作的建议:

myScript.sql:

myScript.sql:



<copy todir="...">
  <fileset dir="...." includes="myScript.sql"/>
  <filterchain>
    <replaceregex byline="false" pattern=";" replace="{line.separator}/" flags="mg"/>
    <replaceregex byline="false" pattern="/[\s]*/" replace=";${line.separator}/"  flags="mg"/>
   </filterchain>
</copy>

then give the result to: <sql delimeter="/" src="myScript.sql"/>

然后将结果提供给: <sql delimeter="/" src="myScript.sql"/>

explaination: If you have regular sql commands:

说明:如果您有常规的 sql 命令:

drop table x;
select blah from blue where bli=blee;

They will be transformed to:

它们将转变为:

drop table x
/
select blah from blue where bli=blee
/

which is equivlant - and the sql command with "/" delimeter can handle them.

这是等效的 - 带有“/”分隔符的 sql 命令可以处理它们。

On the other hand,

另一方面,

BEGIN
  blah
END;
/

will be transformed to:

将转化为:

BEGIN
  blas
END/
/

and using the second regex - transformed back to

并使用第二个正则表达式 - 转换回

BEGIN
  blas
END;
/

So everybody wins! Hurray!

所以大家都赢了!欢呼!

Good luck.

祝你好运。