SQL select all if parameter is null else return specific item

前端 未结 6 1459
终归单人心
终归单人心 2020-12-03 05:29

Is there a way to write the following script so that it returns all products if the ProductID variable is null ? And return a specific product when the product it is not nul

6条回答
  •  情深已故
    2020-12-03 06:24

    Since "" is not recognized as NULL I used value

    SELECT ProductID, ProductName,ProductDesc 
    FROM product 
    WHERE ProductID =IIF(@productID =1, ProductID, @productID )
    

    In my code:

     MyDataAdapter.SelectCommand.Parameters("@productID").Value = 1
    

提交回复
热议问题