如何使用参数获取所有非空行?

时间:2013-08-01 21:55:01

标签: oracle plsql oracle11g

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

2 个答案:

答案 0 :(得分:1)

这样的WHERE子句怎么样:

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

答案 1 :(得分:0)

如果要动态更改查询,请创建参考光标。