SQL use CASE statement in WHERE IN clause

前端 未结 7 1457
南方客
南方客 2020-12-06 00:17

Is it posible to use case in where in clause? Something like this:

 DECLARE @Status VARCHAR(50);
 SET @Status=\'published\';

 SELECT * FRO         


        
7条回答
  •  攒了一身酷
    2020-12-06 00:34

    I believe you can use a case statement in a where clause, here is how I do it:

    Select 
    ProductID
    OrderNo,
    OrderType,
    OrderLineNo
    From Order_Detail
    Where ProductID in (
    Select Case when (@Varibale1 != '') 
    then (Select ProductID from Product P Where .......)
    Else (Select ProductID from Product)
    End as ProductID
    )
    

    This method has worked for me time and again. try it!

提交回复
热议问题