如何分割的Oracle SQL语句,ADO.NET语句、SQL、Oracle、ADO

2023-09-05 00:03:44 作者:浮生泪·殊途同归

什么是正确的方式来分割SQL语句发送到Oracle ADO.NET客户端?举例来说,假设你有以下的code在一个文本文件,并希望执行这些语句:

What is the proper way to split up SQL statements to send to an Oracle ADO.NET client? For instance, lets say you have the following code in a text file and want to execute these statements:

CREATE TABLE foo (bar VARCHAR2(100));
INSERT INTO foo (bar) VALUES('one');
INSERT INTO foo (bar) VALUES('two');

我相信努力将所有这些在一个命令将导致甲骨文抱怨;。我首先想到的是分裂的;性格,给他们一次。

I believe trying to send all those in one Command will cause Oracle to complain about the ";". My first thought would be to split on ";" character, and send them one at a time.

但是,存储过程可以包含分号一样,所以我将如何让这样的分裂程序将保持整个存储过程在一起吗?是否需要寻找开始/结束陈述为好,或/?

But, Stored procedures can contain semi-colons as well, so how would I make it so the split routine would keep the whole stored proc together? Does it need to look for begin/end statements as well, or "/"?

有没有在这些方面ODP.NET和Micrsoft Oracle提供有什么区别?

Is there any difference in these respects between ODP.NET and the Micrsoft Oracle Provider?

推荐答案

如果没有DDL,您可以通过与周围的BEGIN和END语句创建一个匿名PL / SQL块:

Without the DDL, you could create an anonymous PL/SQL block by surrounding the statements with BEGIN and END:

BEGIN
  INSERT INTO foo (bar) VALUES('one');
  INSERT INTO foo (bar) VALUES('two');
END;

要执行DDL(例如CREATE TABLE),你就需要使用动态PL / SQL:

To perform DDL (like CREATE TABLE) you would need to use dynamic PL/SQL:

BEGIN
  EXECUTE IMMEDIATE 'CREATE TABLE foo (bar VARCHAR2(100))';
  EXECUTE IMMEDIATE 'INSERT INTO foo (bar) VALUES(:v)' USING 'one';
  EXECUTE IMMEDIATE 'INSERT INTO foo (bar) VALUES(:v)' USING 'two';
END;

刀片也是动态的,因为表不运行块之前存在,所以它会编译失败。

The INSERTS are also dynamic, as the table does not exist prior to running the block and so it would fail to compile.

请注意:这将是一个不寻常的要求:应用程序一般不应创建表

NOTE: This would be an unusual requirement: applications should not normally be creating tables!