在Oracle嵌套表中查找特定的varchar

发布于 2024-11-15 13:35:49 字数 522 浏览 3 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

樱花坊 2024-11-22 13:35:49

对于单值检查,我更喜欢“成员”运算符

zep@dev> declare
      2      enames     strarray;
      3      wordToFind varchar2(255) := 'King';
      4  begin
      5      select emp.last_name bulk collect
      6      into   enames
      7      from   employees emp;
      8      if wordToFind member of enames then
      9          dbms_output.put_line('Found King');
     10      end if;
     11  end;
     12  /

Found King

PL/SQL procedure successfully completed

zep@dev> 

For single value check I prefer the "member" operator.

zep@dev> declare
      2      enames     strarray;
      3      wordToFind varchar2(255) := 'King';
      4  begin
      5      select emp.last_name bulk collect
      6      into   enames
      7      from   employees emp;
      8      if wordToFind member of enames then
      9          dbms_output.put_line('Found King');
     10      end if;
     11  end;
     12  /

Found King

PL/SQL procedure successfully completed

zep@dev> 
2024-11-22 13:35:49

您可以使用 MULTISET INTERSECT 运算符来确定集合中是否存在您感兴趣的字符串。例如

declare
  l_enames strarray;
  l_interesting_enames strarray := new strarray( 'KING' );
begin
  select ename
    bulk collect into l_enames
    from emp;
  if( l_interesting_enames = l_interesting_enames MULTISET INTERSECT l_enames )
  then
    dbms_output.put_line( 'Found King' );
  end if;
end;

,如果字符串“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

declare
  l_enames strarray;
  l_interesting_enames strarray := new strarray( 'KING' );
begin
  select ename
    bulk collect into l_enames
    from emp;
  if( l_interesting_enames = l_interesting_enames MULTISET INTERSECT l_enames )
  then
    dbms_output.put_line( 'Found King' );
  end if;
end;

will print out "Found King" if the string "KING" is an element of the l_enames collection.

再见回来 2024-11-22 13:35:49

当嵌套表被声明为架构级别类型时(正如您所做的那样),它可以在任何 SQL 查询中作为表使用。所以你可以编写一个简单的函数,如下所示:

CREATE OR REPLACE FUNCTION exists_in( str VARCHAR2, tab stararray)
  RETURN BOOLEAN
  AS
    c  INTEGER;
  BEGIN
    SELECT COUNT(*)
      INTO c
      FROM TABLE(CAST(tab AS strarray))
      WHERE column_value = str;
    RETURN (c > 0);
  END exists_in;

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:

CREATE OR REPLACE FUNCTION exists_in( str VARCHAR2, tab stararray)
  RETURN BOOLEAN
  AS
    c  INTEGER;
  BEGIN
    SELECT COUNT(*)
      INTO c
      FROM TABLE(CAST(tab AS strarray))
      WHERE column_value = str;
    RETURN (c > 0);
  END exists_in;
鱼忆七猫命九 2024-11-22 13:35:49

如果您想确定集合中是否存在该元素,则应该将数组索引而不是数组值传递给 exists。嵌套表按整数索引,因此无法通过字符串引用它们。

但是,如果您希望通过字符串索引引用数组元素,您可能需要查看关联数组而不是集合。这看起来像这样:

DECLARE
  TYPE assocArray IS TABLE OF VARCHAR2(100) INDEX BY VARCHAR2(100);
  myArray assocArray;
BEGIN

  myArray('foo') := 'bar';

  IF myArray.exists('baz') THEN
    dbms_output.put_line(myArray('baz'));

  ELSIF myArray.exists('foo') THEN
    dbms_output.put_line(myArray('foo'));

  END IF;

END;

基本上,如果您的数组值不同,您可以创建相互引用的配对数组,例如,
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:

DECLARE
  TYPE assocArray IS TABLE OF VARCHAR2(100) INDEX BY VARCHAR2(100);
  myArray assocArray;
BEGIN

  myArray('foo') := 'bar';

  IF myArray.exists('baz') THEN
    dbms_output.put_line(myArray('baz'));

  ELSIF myArray.exists('foo') THEN
    dbms_output.put_line(myArray('foo'));

  END IF;

END;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文