1

以下代码给出“PL/SQL:ORA-00907:缺少右括号”。为什么?

select count(*)
into v_cnt
from (table(v_A) intersect table(v_B));

尽管

select count(*)
into v_cnt
from table(v_A);

编译没有问题。v_A 属于 t_A 类型,由:

create or replace type t_A is table of varchar2(1 byte);
4

2 回答 2

2

您必须首先将两个结果集相交,然后计算元素,如下所示:

   select count(*)
   into   l_cnt
   from 
      (
         select * 
         from   table(v_A) 
      intersect 
         select * 
         from   table(v_B)
      );
于 2013-01-10T19:32:51.317 回答
2

如果您在 PL/SQL 中执行此操作,您可能应该使用MULTISET INTERSECT.

它比使用 SQL 更简单,速度也快几个数量级。

通常使用 SQL 更简单、更快捷。但是在这种情况下,由于您已经在 PL/SQL 中,所以情况正好相反。您希望尽可能避免在它们之间切换。

--SQL Method: 8.5, 8.455, 8.502 seconds
--PL/SQL Method: 0.015, 0.016, 0.016 seconds
declare
    v_cnt number;
    v_a t_a := t_a('a','b','c');
    v_b t_a := t_a('a','b');
    v_c t_a;
begin
    for i in 1 .. 100000 loop
        --SQL method
        /*
        select count(*) into v_cnt
        from 
        (
            select * from table(v_A) 
            intersect 
            select * from table(v_B)
        );
        */

        --PL/SQL method
        v_c := v_a multiset intersect v_b;
        v_cnt := v_c.count;
    end loop;

    dbms_output.put_line(v_cnt);
end;
/
于 2013-01-10T20:40:12.087 回答