正确使用收集方法

OTA*_*TAR 2 oracle plsql user-defined-types oracle11g

CREATE  OR  REPLACE  TYPE  nvarchar2_list_type AS TABLE OF NVARCHAR2(100);


CREATE   TABLE test_table(
   id number primary key,
   cars_list     nvarchar2_list_type 
)
NESTED TABLE cars_list  STORE AS cars_list_storage_table;


insert into test_table(id, cars_list)
    values(1,  nvarchar2_list_type( 'AUDI', 'MERCEDES')  );
Run Code Online (Sandbox Code Playgroud)

以上所有操作都成功完成,在test_table表中插入1行,现在我写了这个函数:

create or replace function get_cnt 
return number
as
  ret_val number;
begin
  SELECT  cars_list.COUNT    
    INTO  ret_val
   from test_table where id  = 1;

   return ret_val;
end;
Run Code Online (Sandbox Code Playgroud)

这给出了错误: ORA-00904: "CARS_LIST"."COUNT": invalid identifier

请告诉我这里有什么问题?

据我所知,COUNT必须使用方法(从这里)

Nic*_*nov 5

不,你不能count在这种情况下使用方法.您手头有SQL嵌套表,count方法仅用于PL/SQL集合.

要计算嵌套表元素的数量,您可以取消嵌套表或使用标量子查询:

Unnesting:

SQL> select id
  2       , count(*) as cnt
  3     from test_table t
  4     cross join table(t.cars_list)
  5    group by id
  6  ;

        ID        CNT
---------- ----------
         1          2
Run Code Online (Sandbox Code Playgroud)

标量子查询:

SQL> select id
  2       , (select count(column_value)
  3            from table(t.cars_list)) as cnt
  4     from test_table t
  5  ;
        ID        CNT
---------- ----------
         1          2
Run Code Online (Sandbox Code Playgroud)