Conditional WHERE clause in SQL Server

后端 未结 6 1560
猫巷女王i
猫巷女王i 2020-11-22 12:43

I am creating a SQL query in which I need a conditional where clause.

It should be something like this:

SELECT 
    DateAppr,
    Time         


        
6条回答
  •  独厮守ぢ
    2020-11-22 13:10

    This seemed easier to think about where either of two parameters could be passed into a stored procedure. It seems to work:

    SELECT * 
    FROM x 
    WHERE CONDITION1
    AND ((@pol IS NOT NULL AND x.PolicyNo = @pol) OR (@st IS NOT NULL AND x.State = @st))
    AND OTHERCONDITIONS
    

提交回复
热议问题