【问题标题】:How to use in statement with nested table如何在嵌套表中使用 in 语句
【发布时间】:2015-11-02 23:57:11
【问题描述】:

嘿,我有一个函数,该函数的一部分是确保所选值在传入的 varchar2s 表中。首先,我声明一个 varchar2 表类型,如下所示。

create or replace type Varchar2Table is table of varchar2(200)

然后我有一个函数,它接受嵌套表参数并在它们上有一个 select 语句。

function SelectPeople(inputNames Varchar2Table) return People
begin
--stuff
select * from person_table where name in inputNames; --line of interest
--more stuff
end;

这似乎不起作用,我收到以下错误:

ORA-00932:不一致的数据类型:得到预期的 NUMBER ENGSPL5.VARCHAR2TABLE

有什么建议吗?

【问题讨论】:

  • 您是否在此处收到错误消息?如果是这样,是什么?如果没有,会发生什么?
  • 错误是“ORA-00932:不一致的数据类型:预期的 NUMBER 得到了 ENGSPL5.VARCHAR2TABLE”。不知道为什么当我在两边都传递一个 varchar2 时它会显示预期的数字。
  • 尽管您没有在双方都传递 VARCHAR2,但您要求它将 VARCHAR2 列转换为它没有重载的用户定义类型。我认为 NUMBER 一定是它的默认猜测。

标签: sql oracle plsql


【解决方案1】:

TABLE 运算符允许在 SQL 语句中使用嵌套表。该函数还缺少ISINTO

create or replace type Varchar2Table is table of varchar2(200);

create table person_table(id number, name varchar2(100));

create or replace function SelectPeople(inputNames Varchar2Table) return number
is --Missing "IS".
    type numberTable is table of number; --Need a collection to store results.
    numbers numberTable;
begin
    select id
    bulk collect into numbers --Missing "INTO".
    from person_table
    where name in (select column_value from table(inputNames)); --Missing "TABLE".
    --Alternatively a multiset condition can be used.
    --where name member of inputNames;

    --Dummy return value to make the function compile.
    return 1;
end;
/

【讨论】:

  • 你也可以使用WHERE name MEMBER OF inputNames
猜你喜欢
  • 2019-07-16
  • 1970-01-01
  • 2022-12-31
  • 2012-10-15
  • 2016-10-22
  • 2012-04-26
  • 2012-09-26
  • 2012-02-20
  • 2021-04-15
相关资源
最近更新 更多