2017-07-06 65 views
0

我在使用可变参数挣扎存储过程必须使用在其WHERE子句中传递给它的每个参数对表执行SELECT操作。Oracle SQL - 具有可变参数的SELECT存储过程

基本上我有N帐号作为参数,我想返回一个表,每个帐号选择三个字段的结果。

这是我迄今所做的:

function sp_get_minutes_expiration_default(retval IN OUT char, gc IN OUT GenericCursor, 
     p_account_num IN CLIENT_ACCOUNTS.ACCOUNT_NUM%TYPE) return number 
     is 
    r_cod integer := 0; 
    begin 
     open gc for select account_num, concept_def, minutes_expiration_def from CLIENT_ACCOUNTS 
      where p_account_num = account_num; -- MAYBE A FOR LOOP HERE? 
     return r_cod; 
    exception 

     -- EXCEPTION HANDLING 

    end sp_get_minutes_expiration_default; 

我的蛮力解决方案是可能遍历帐号,选择列表,也许做一个UNION或追加到结果表?

回答

1

如果你投你的输入参数作为一个表,那么你可以加入到CLIENT_ACCOUNTS

select account_num, concept_def, minutes_expiration_def 
from CLIENT_ACCOUNTS ca, table(p_account_num) a 
where a.account_num = ca.account_num 

但我会建议你选择输出到另一个集合,是函数(或程序)的输出。我会敦促你不要使用引用游标。

附录1

一个更完整的示例如下:

create or replace type id_type_array as table of number; 
/

declare 
    ti id_type_array := id_type_array(); 
    n number; 
begin 
    ti.extend(); 
    ti(1) := 42; 
    select column_value into n from table(ti) where rownum = 1; 
end; 
/

在你的代码,你将需要使用框架的API:

  1. 创建集合的一个实例(类型id_type_array)
  2. 用数字列表填充集合
  3. 执行匿名PL/SQL块,收集

的约束力,但你应该立即看到你没有把查询到的匿名PL/SQL块来执行它(即使许多有经验的Oracle开发者提倡它)。只要您绑定正确的参数,您可以执行查询,就像任何其他查询:

select account_num, concept_def, minutes_expiration_def 
from CLIENT_ACCOUNTS ca, table(:p_account_num) a 
where a.column_value = ca.account_num 
+0

@ jeff5times7在这短短的例子p_account_num应该是什么类型,SQL LIST的?我一直在阅读关于TABLE()函数的内容,但我不太明白它需要什么参数。请你再扩充一点吗? – Franch

+0

请参阅附录1。 – jeff6times7