在WHERE子句中包含条件AND

时间:2012-04-20 15:33:12

标签: sybase

我们可以在WHERE子句中的一个过程参数上添加空检查,并避免代码块重复,如

select aa=t1.aa,bb=t2.bb ,cc=t3.cc
 from t1,t2,t3,t4,t5
where t1.p1=t2.p1
and t2.p2=t3.p2
 IF(procParameter IS NOT NULL)
   and t3.p2=procParameter 
and t4.p2=t5.p2
and t5.p3=t1.p2 

看起来我希望其中一个AND有条件地执行,否则它不应该全部执行.. !!!!

我应该如何进行此优化? 我不想像

那样重复代码
IF(procParameter IS NOT NULL)
begin
  select aa=t1.aa,bb=t2.bb ,cc=t3.cc
    from t1,t2,t3,t4,t5
     where t1.p1=t2.p1
    and t2.p2=t3.p2
    and t3.p2=procParameter 
     and t4.p2=t5.p2
    and t5.p3=t1.p2 
end
Else
begin
  select aa=t1.aa,bb=t2.bb ,cc=t3.cc
    from t1,t2,t3,t4,t5
     where t1.p1=t2.p1
    and t2.p2=t3.p2
     and t4.p2=t5.p2
    and t5.p3=t1.p2 
end

谢谢,

1 个答案:

答案 0 :(得分:1)

这是一种方法:

AND (procParameter IS NULL OR t3.p2=procParameter)
相关问题