mardi 30 juin 2015

Check a certain field for not null based on a parameter in sql

I get data from a stored procedure. Based on a passed Parameter I want to Change the SQL Statement in the where clause

The Statement Looks like this

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE CC.TId IS NOT NULL

I pass a Parameter @Qualifier to the procedure so I want to check this param. If it is '1' this SQL Statement should be executed, otherwise the second SQL Statement should be executed:

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE CC.PId IS NOT NULL

I tried to achieve this using the where clause like this

    SELECT 
      CC.Id,
      CC.TId,
      CC.PId,
      CC.Date
    FROM Mytable CC
    WHERE 
     (CASE
WHEN @Qualifier = '1'
  THEN CC.TId IS NOT NULL
ELSE CC.PId IS NOT NULL)

But that is not working. Anyone knows how to solve this?

Aucun commentaire:

Enregistrer un commentaire