在oracle数据库中,有一个表table1,表中有一个字段a,在a有创建了一个索引。现有一个SQL如下:
select * from table1 where a = :v_1;
其中v_1是一个字符型参数,如果这个参数传入的值是一个空值,则返回所有的数据(如果按照上面的Sql是不能达到的)应该怎修改上面的SQL,即不破坏a的索引,又能很好的处理空值问题。
      
     请赐教!

解决方案 »

  1.   

    select * from table1 where 1=case when v_1 is null then 1 else 0 or a=v_1
      

  2.   

    select * from table1 where 1=(case when v_1 is null then 1 else 0 end ) or a=v_1
      

  3.   


    sqlstr:=‘select * from table1 ’ || case when v_1 is not null then ‘where a = :v_1' end;
      

  4.   

    select * from table1 where a = :v_1 or :v_1 is null  不知道这样可以不 v_1 is null 时候 取全部数据 应该走全表扫描 
    v_1 is not null 时 是走索引
      

  5.   

    select * from table1 where 
    a=(case when v_1 is not null then v_1 end) or
    1=(case when v_1 is  null then 1 end)else 1=1 end
      

  6.   

    select * from table1 where a = :v_1 or :v_1 is null也可以动态拼sql,如果:v_1是null就不在sql中拼这个条件