我需要使用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