2013-08-01 48 views
2

This SQL Server Post非常有幫助。我在Oracle 11g上使用PL/SQL。我希望光標同時接受v_parm1v_parm2如何使用參數獲取所有非空行?

這是我所做的。

DECLARE 
v_parm1 varchar2(50) := 'TALC'; 
v_parm2 varchar2(50) := '*'; 

cursor cursor_with_parms(in_parm_list varchar2) 
is 
with mohs_scale as 
(select '1' as moh_val , 'TALC' as mineral from dual union all 
select '2' as moh_val , 'GYPS' as mineral from dual union all 
select '3' as moh_val , 'CALC' as mineral from dual union all 
select '4' as moh_val , 'FLUO' as mineral from dual) 
select moh_val, 
     mineral 
from mohs_scale 
where 
    case in_parm_list when '*' then mineral is not null 
    else mineral = in_parm_list 
end;  

BEGIN 
for rock in cursor_with_parms(v_parm1) loop 
dbms_output.put_line(rock.moh_val || ' ' || rock.mineral); 
end loop; 
END; 

如果我使用v_parm1,那麼只返回1-TALC。 如果我使用v_parm2,則返回所有4行。

問題:如何寫光標接受v_parm1v_parm2

回答

1

什麼WHERE子句這樣的:

WHERE 
     (in_parm_list = '*') 
    OR (mineral = in_parm_list) 
0

如果要動態更改查詢,請創建Ref光標。

相關問題