在Oracle嵌套表中查找特定的varchar
我是 PL-SQL 新手,正在努力寻找嵌套表操作的清晰文档。请更正任何误用的术语等。
我有一个嵌套表类型,用作存储过程的参数。
CREATE OR REPLACE TYPE "STRARRAY" AS TABLE OF VARCHAR2 (255)
在我的存储过程中,该表被初始化并填充。假设我有一个 VARCHAR2 变量,我想知道该 varchar 是否存在于嵌套表中。
我尝试过
strarray.exists('somevarchar')
,但收到 ORA-6502
除了迭代之外还有更简单的方法吗?
FOR i IN strarray.FIRST..strarray.LAST
LOOP
IF strarray(i) = value THEN
return 1;--found
END IF;
END LOOP;
I'm new to PL-SQL, and struggling to find clear documentation of operations are nested tables. Please correct any misused terminology etc.
I have a nested table type that I use as a parameters for a stored procedure.
CREATE OR REPLACE TYPE "STRARRAY" AS TABLE OF VARCHAR2 (255)
In my stored procedure, the table is initialized and populated. Say I have a VARCHAR2 variable, and I want to know true or false if that varchar exists in the nested table.
I tried
strarray.exists('somevarchar')
but I get an ORA-6502
Is there an easier way to do that other than iterating?
FOR i IN strarray.FIRST..strarray.LAST
LOOP
IF strarray(i) = value THEN
return 1;--found
END IF;
END LOOP;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于单值检查,我更喜欢“成员”运算符。
For single value check I prefer the "member" operator.
您可以使用 MULTISET INTERSECT 运算符来确定集合中是否存在您感兴趣的字符串。例如
,如果字符串“KING”是 l_enames 集合的元素,则会打印出“Found King”。
You can use the MULTISET INTERSECT operator to determine whether the string you're interested in exists in the collection. For example
will print out "Found King" if the string "KING" is an element of the l_enames collection.
当嵌套表被声明为架构级别类型时(正如您所做的那样),它可以在任何 SQL 查询中作为表使用。所以你可以编写一个简单的函数,如下所示:
When a nested table is declared as a schema-level type, as you have done, it can be used in any SQL query as a table. So you can write a simple function like so:
如果您想确定集合中是否存在该元素,则应该将数组索引而不是数组值传递给
exists
。嵌套表按整数索引,因此无法通过字符串引用它们。但是,如果您希望通过字符串索引引用数组元素,您可能需要查看关联数组而不是集合。这看起来像这样:
基本上,如果您的数组值不同,您可以创建相互引用的配对数组,例如,
arr('b') := 'a'; arr('a') := 'b';
这种技术可以帮助您轻松查找任何元素及其索引。
You should pass an array index, not an array value to an
exists
in case you'd like to determine whether this element exists in collection. Nested tables are indexed by integers, so there's no way to reference them by strings.However, you might want to look at associative arrays instead of collections in case you wish to reference your array element by string index. This will look like this:
Basically, if your array values are distinct, you can create paired arrays referencing each other, like,
arr('b') := 'a'; arr('a') := 'b';
This technique might help you to easily look up any element and its index.