1

我们可以在 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

谢谢,

4

1 回答 1

1

这是一种方法:

AND (procParameter IS NULL OR t3.p2=procParameter)
于 2012-04-20T15:36:03.410 回答