如何使用 .NET 执行多个 Oracle SQL 语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/528676/
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
How can I execute multiple Oracle SQL statements with .NET
提问by devdimi
As a part of our build process we want to execute SQL Scripts consisting of DDL and DML statements against a fresh database instance.
作为构建过程的一部分,我们希望针对新的数据库实例执行由 DDL 和 DML 语句组成的 SQL 脚本。
ADO.NET Connection/Command can't handle this without parsing and splitting the scripts.
ADO.NET Connection/Command 无法在不解析和拆分脚本的情况下处理此问题。
The sqlplus
command line utility can execute scripts only interactively, and isn't suited for batch usage.
该sqlplus
命令行实用程序只能执行交互脚本,以及不适合于批料的使用。
What am I missing? How can one execute sql scripts when using oracle?
我错过了什么?使用oracle时如何执行sql脚本?
采纳答案by Justin Cave
Why do you believe that the SQLPlus utility isn't suited for batch usage? It's pretty common to use it for running these sorts of scripts-- you can pass the script to SQLPlus when you invoke it if you'd like, i.e.
为什么您认为 SQL Plus 实用程序不适合批处理使用?使用它来运行这些类型的脚本是很常见的——如果你愿意,你可以在调用它时将脚本传递给 SQLPlus,即
sqlplus scott/tiger@someDatabase @someScript.sql
That is a pretty common way of deploying builds.
这是部署构建的一种非常常见的方式。
If the problem is with the way SQL*Plus is handling errors, you can simply add the line
如果问题出在 SQL*Plus 处理错误的方式上,您只需添加以下行
WHENEVER SQLERROR EXIT SQL.SQLCODE
to abort and throw the Oracle error number that was encountered. The documentation for the WHENEVER SQLERROR commandprovides a number of other options as well.
中止并抛出遇到的 Oracle 错误号。WHENEVER SQLERROR 命令的文档还提供了许多其他选项。
回答by Justin Cave
Devdimi,
德迪米,
I agree with Erik K.
我同意 Erik K.
And yes you can include DDL in an anonymous block.
是的,您可以在匿名块中包含 DDL。
DECLARE
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE foo';
END;
Remember DDL does a commit BEFORE and AFTER it runs. So as part of a transaction it kinda sucks.
请记住,DDL 在运行之前和之后都会执行一次提交。所以作为交易的一部分,它有点糟糕。
回答by erikkallen
I think you can do it if you wrap the statement inside DECLARE/BEGIN/END. I don't have access to Oracle anymore so I can't test it, though. Example:
我认为如果您将语句包装在 DECLARE/BEGIN/END 中,您就可以做到。不过,我无法再访问 Oracle,因此无法对其进行测试。例子:
DECLARE
BEGIN
INSERT INTO ...;
UPDATE something ...;
END;
Since you want to execute DDL, use EXECUTE IMMEDIATE
.
既然要执行 DDL,就用EXECUTE IMMEDIATE
.