我试图在IN子句中使用一个输入值数组作为游标的where子句的一部分.我知道之前已经问过,但我还没有看到如何正确编译语法.
在包规范中,类型是
TYPE t_brth_dt IS TABLE OF sourceTable.stdt_brth_dt%TYPE INDEX BY PLS_INTEGER;
sourceTable.std_brth_dt是表中的日期列.
我的光标的简化版本是在包体中 –
cursor DataCursor_Sort( p_brth_dt in t_brth_dt) is SELECT * FROM sourceTable WHERE a.brth_dt IN (select column_value from table(p_brth_dt))
当我尝试编译它时,我收到以下错误.
- [1]:(Error): PLS-00382: expression is of wrong type
- [2]:(Error): PL/sql: ORA-22905: cannot access rows from a non-nested table item
我知道这看起来与其他问题类似,但我不明白语法错误是什么.
解决方法
为了在查询的from子句中使用定义为嵌套表或关联数组的集合,您应该正如@Alex Poole正确指出的那样,创建模式级别(sql)类型或使用一个,您可以使用的类型通过ODCIConst包 – odcidatelist,因为您打算使用日期列表.例如,您的游标定义可能如下所示:
cursor DataCursor_Sort(p_brth_dt in sys.odcidatelist) is select * from sourceTable where a.brth_dt IN (select column_value from table(p_brth_dt))
要么
cursor DataCursor_Sort(p_brth_dt in sys.odcidatelist) is select s.* from sourceTable s join table(p_brth_dt) t on (s.brth_dt = t.column_value)
注意:您应该在执行日期比较时考虑日期的时间部分.如果你只想比较日期部分,那么通过使用trunc()函数去除时间部分可能会有用.