2013-01-07 51 views
1

我在oracle数据库中使用plsql 9i 我有一个存储过程, 使用一个名为numbers的“number of table”的in参数。 我现在想要从列表中选择所有行,列名为:ID等于“数字”内的数字 就像我能做的那样select * from table name where Id in (!,!,!,...) 感谢您的帮助。在一个过程中使用plsql表

更新: 只是为了清理, 我有一个用户定义的类型命名号码, 号码定义:数字表。 所以在程序decleration我有 “P_array在数字” 我需要select * from a table where Id is found in p_array

回答

0
SELECT * FROM A_TABLE WHERE ID IN (SELECT SAVED_NUMBERs FROM TABLE_NUMBER); 

你的意思呢?

+0

我试过“SELECT * FROM atable其中Id的(选择数saved_numbers)。我得到ORA 04044错误 – user1333057

+0

从头开始,我尝试 “SELECT * FROM表名其中id中(从myArrayName选择saved_numbers)”。我得到了一个00942 ora错误 – user1333057

+0

等等,所以你有一个包含所有数字的数组?如果是这种情况,请尝试:SELECT * FROM WHERE ID IN ; –

1

尝试以下操作: -

select * from tableName where id in (select c.column_value from table(cast(p_array as numbers)) c); 

其中数字是编号为

+0

这个编译,但我得到一个ora 22905当exceling的 – user1333057

+0

编辑我的代码... – Max

2

这样的表?

SQL> create type numbers as table of number; 
    2/

Type created. 

SQL> create table foo (id number) ; 

Table created. 

SQL> insert into foo select rownum from dual connect by level <= 10; 

10 rows created. 

SQL> select * from foo; 

     ID 
---------- 
     1 
     2 
     3 
     4 
     5 
     6 
     7 
     8 
     9 
     10 

10 rows selected. 

SQL> create procedure testnum(p_num in numbers) 
    2 is 
    3 begin 
    4 for r_row in (select id 
    5     from foo f 
    6     where f.id in (select /*+ cardinality(t, 10) */ column_value 
    7         from table(p_num) t)) 
    8 loop 
    9  dbms_output.put_line(r_row.id); 
10 end loop; 
11 end; 
12/

Procedure created. 

SQL> set serverout on 
SQL> exec testnum(numbers(2, 6, 9)); 
2 
6 
9 

基数提示用于告诉oracle大致有多少元素在你的表中。没有它,Oracle会假设〜8k行,这可能太高,并导致计划中的不必要的全面扫描。

如果您愿意,也可以直接加入。

for r_row in (select /*+ cardinality(t, 10) */ f.id 
       from foo f 
        inner join table(p_num) t 
          on t.column_value = f.id)