mercredi 30 janvier 2019

MS SQL IF statement in WHERE clause

I can't seem to get the IF statement to work in my WHERE clause for MS SQL.

Basically, if @PK is 0, I want to skip those two lines in the IF statement.

Here is what I have:

WHERE
  IF (@PK <> 0)
    pk.ID >= @PK
    AND pk.ID < (@PK + 500)
  ELSE
    Set_No = Set_No
  END
  AND Set_No IN (1,2,3,4)

Also tried:

WHERE
  CASE WHEN @PK <> 0
     pk.ID >= @PK
    AND pk.ID < (@PK + 500)
  ELSE
    Set_No = Set_No
  END
  AND Set_No IN (1,2,3,4)

Edit: I searched SO and tried several things I found (including above), but they didn't work.

Aucun commentaire:

Enregistrer un commentaire