select * from so_sale where decode(#pkcorp#,null,'1',so_sale_pk_corp) in (decode(#pkcorp#,null,'1',#pkcorp#))  
这个语句中#pkcorp#这个参数单值的形式是(‘a’),多值的话是(‘a’,‘b’)  
要想实现的功能就是当#pkcorp#值为空的时候,就没有查询条件(相当于1=1),能全部查出来,如果有值的话(假设值为a)  
那就相当于select * from so_sale where so_sale_pk_corp in(‘a’)  
我现在碰到的问题是为多值的时候,decode语句不起作用。select * from so_sale where decode(#pkcorp#,null,'1',so_sale_pk_corp) in (decode(#pkcorp#,null,'1',#pkcorp#))   
这个语句就通不过,实在是搞不明白。  
请问有办法解决么。其实我想要的结果很简单,对于decode(#pkcorp#,null,'1',so_sale_pk_corp) in (decode(#pkcorp#,null,'1',#pkcorp#)这个语句  
就是判断#pkcorp#是否有值,如果有值的话就返回 so_sale_pk_corp in (#pkcorp#) 反之就返回 1=1 也就是全部返回就是想得到这个结果,但是当#PKCORP#为多值的时候,比如(‘a’,‘b’)就是无法查出来。。请问能否实现呢?

解决方案 »

  1.   

    字符串的'a','b'和单独输入的'a','b'是不一样的
    前者是认为是一个值
    做比较时a<> 'a','b'
    b<>' 'a','b' 
    a,b<>'a','b'
    'a','b'='a','b'
    所以你这需要用到动态SQL
    execute immediate 'select * from so_sale where decode('||#pkcorp#||',null,'1',so_sale_pk_corp) in (decode('||#pkcorp#||',null,'1','||#pkcorp#||'))'   
      

  2.   

    不需要考虑效率的话:
    select * from so_sale 
    where 
      decode(#pkcorp#,null,1,0) > 0 OR instr(#pkcorp#,''''|| so_sale_pk_corp || '''')) > 0
      

  3.   

    select * from so_sale   
    where   nvl2(#pkcorp#,1,so_sale_pk_corp) in nvl(#pkcorp#,1)
      

  4.   

    没看到多值的话
    select * from so_sale    
    where   ','||nvl(#pkcorp#,1)||',' like   '%,'||nvl2(#pkcorp#,1,so_sale_pk_corp)||',%'