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.   

    用case when也不好用吗? 
    select * from so_sale where (case #pkcorp# when null then '1' else so_sale_pk_corp end ) in (case #pkcorp# when null then '1' else #pkcorp# end)
    没有试过。猜想可能可以。
      

  2.   

    无法这样使用,只能通过判断.
    为空 1=1,
    不为空要使用动态SQL.
      

  3.   

    最简单的办法是将decode的内容更改为函数处理。
      

  4.   

    这个语句明显不对,你不用考虑这么复杂,where #xxx is null or #xxx in('a')这样不就可以了
      

  5.   

    select * from so_sale 
    where decode(#pkcorp#,null,'1',so_sale_pk_corp) 
    in (decode(#pkcorp#,null,'1',#pkcorp#))  
    为空的话默认为1 条件这样带进去
    where (decode(#pkcorp#,null,'1',so_sale_pk_corp) in (decode(#pkcorp#,null,'1',#pkcorp#))  
      or #pkcorp#=1)