Oracle关联数组存在导致编译失败的case语句



这是一个假设的例子。实际的问题涉及到基于值(如果它们存在于关联数组中)更新各种列。下面抛出ORA-06550和ORA-01747。请帮忙修复这个错误。

declare
   type MONTH_TYPE is table of varchar2(20) index by binary_integer;
   month_table   MONTH_TYPE;
   mon varchar2(20);
 begin
   month_table(1) := 'Jan';
   month_table(2) := 'Feb';
   select case when month_table.exists(1) then 'found' else 'not found' end into mon from dual;
 end;

不能从SQL语句中调用PL/SQL exists函数。如果需要,可以引用集合中的:

declare
   type MONTH_TYPE is table of varchar2(20) index by binary_integer;
   month_table   MONTH_TYPE;
   mon varchar2(20);
 begin
   month_table(1) := 'Jan';
   month_table(2) := 'Feb';
   select case when month_table(1)='Jan' then 'found' else 'not found' end
        into mon from dual;
end;

也可以在PL/SQL中使用exists:

declare
   type MONTH_TYPE is table of varchar2(20) index by binary_integer;
   month_table   MONTH_TYPE;
   mon varchar2(20);
 begin
   month_table(1) := 'Jan';
   month_table(2) := 'Feb';
   mon := case when month_table.exists(1) then 'found' else 'not found' end;
end;

从你的评论来看,数据库类型可能是可行的方法:

SQL> create type MONTH_TYPE is table of varchar2(20);

然后你可以在SQL中选择:

declare
   month_table   MONTH_TYPE := MONTH_TYPE();
   mon varchar2(20);
 begin
   month_table.extend;
   month_table(1) := 'Jan';
   month_table.extend;
   month_table(2) := 'Feb';
   update some_table
   set x = 1
   where month in (select column_value from table(month_table));
end;

最新更新