sql-server – 如何在SQL中使用Where语句Where子句?

前端之家收集整理的这篇文章主要介绍了sql-server – 如何在SQL中使用Where语句Where子句?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要使用if语句里面的where子句在sql中.
Select * from Customer
WHERE  (I.IsClose=@ISClose OR @ISClose is NULL)  
AND    
(C.FirstName like '%'+@ClientName+'%' or @ClientName is NULL )    
AND 
 if (@Value=2)
  begin
  (I.RecurringCharge=@Total  or @Total is NULL )    
  end
 else if(@Value=3)
begin
(I.RecurringCharge like '%'+cast(@Total as varchar(50))+'%' or @Total is NULL )  
end

注意:这不是完整的代码.在SP.I中定义了一切.只需编写需要了解该问题的代码.

提前致谢.

解决方法

你必须使用 CASE Statement/Expression
Select * from Customer
WHERE  (I.IsClose=@ISClose OR @ISClose is NULL)  
AND    
    (C.FirstName like '%'+@ClientName+'%' or @ClientName is NULL )    
AND 
     CASE @Value
         WHEN 2 THEN (CASE I.RecurringCharge WHEN @Total or @Total is NULL) 
         WHEN 3 THEN (CASE WHEN I.RecurringCharge like 
                               '%'+cast(@Total as varchar(50))+'%' 
                     or @Total is NULL )
     END
原文链接:https://www.f2er.com/mssql/82035.html

猜你在找的MsSQL相关文章