如何使用 SQL IN (或“ANY”)运算符与 PL/SQL 中的 VARRAY
我的.NET代码当前正在使用ODP.NET多次调用存储过程来对许多表中的各个行进行操作。 .NET 代码有一个要更改的行数组。每次调用中只有一个参数发生变化,我想将数组从.NET传递到PL/SQL以对多行进行操作(行数将会改变)。
我已经使用以下方法成功地将数组从.NET传递到PL/SQL:
type number_arr is table of number(10) index by pls_integer;
PROCEDURE "BLAH" (foo IN number_arr);
请注意,我相信number_arr被称为VARRAY,但我对此并不肯定,如果有人想纠正我,请做(作为评论) ,但这可能会导致我的困惑。
但现在,在 PL/SQL 中,我有许多过去看起来像这样的更新语句:
UPDATE t SET a = b WHERE a = foo;
when foo isn't an array。我现在想写:
UPDATE t SET a = b WHERE a IN (foo);
但是这个语法似乎不起作用。我一直无法找到结合使用 VARRAY 和“IN”(或“ANY”等)的 Oracle 示例。我已经看到了一些关于如何使用 SQL Server 执行此操作的答案,但我不确定如何将其转换为 Oracle。
当然,如果有其他方法可以将数组从 .NET 获取到存储过程来执行此操作,那也可以回答我的问题。我希望通过 IN 提高效率,因此在 PL/SQL 中对数组进行迭代(单独调用 UPDATE 语句)的操作可能不会有帮助。
My .NET code is currently using ODP.NET to call a stored procedure many times to operate on various rows in many tables. The .NET code has an array of the rows to change. Only one parameter changes in each call, and I'd like to pass the array from .NET to the PL/SQL to operate on multiple rows (the number of rows will change).
I've successfully passed an array from .NET to PL/SQL using:
type number_arr is table of number(10) index by pls_integer;
PROCEDURE "BLAH" (foo IN number_arr);
Note that I believe number_arr is called a VARRAY, but I'm not positive about that, and if someone wants to correct me, please do (as a comment), but this might be contributing to my confusion.
But now, in the PL/SQL, I have many update statements that used to look like:
UPDATE t SET a = b WHERE a = foo;
when foo wasn't an array. I now want to write:
UPDATE t SET a = b WHERE a IN (foo);
But this syntax doesn't seem to work. And I've been unable to find an example for Oracle that combines use of VARRAY and 'IN' (or 'ANY', etc.). And I've seen some answers for how to do this with SQL Server, but I'm not sure how to translate that to Oracle.
Of course, if there is some other way to get the array from .NET to the stored procedure to do this, that would also answer my question. I'm looking to gain efficiency with the IN, so something that itterates over the array in PL/SQL (to call the UPDATE statements separately) probably won't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的数组是关联数组< /a>,不是变量。可变数组和嵌套表可以在 SQL 中使用,但关联数组不能。但是,由于您首先尝试在 PL/SQL 中执行此操作,因此可以使用批量绑定(它将与关联数组一起使用):
如果您在数据库中将 number_arr 创建为 varray 而不是关联数组,您可以改用表函数:
请注意,在这种情况下,类型必须在数据库中定义,而不是在包中定义。此外,此方法不一定比使用
forall
更快。The array you're using is an associative array, not a varray. varrays and nested tables can be used in SQL, but associative arrays cannot. However, since you're trying to do this in PL/SQL in the first place, you can use a bulk bind (which will work with an associative array):
If you created number_arr as a varray in the database instead of an associative array, you could use the table function instead:
Note that, in this case, the type has to be defined in the database, not in your package. Also, this method won't necessarily be any faster than using
forall
.