vendredi 24 juillet 2020

filtering a parameter in a where clause using an if/case statement

I have two queries as such

select *
from table 1
where id=145

query 2

select *
from table 1
where code=452 and typeCode <> 3

How do i filter a where condition based on the parameter value,for example i have the following parameter that can be a "History" or "latest" as such

Declare @Type nvarchar(10)
set @Type='History'

so now i want to say in my where clause if type = history than do the first query if its equal to latest than do the second query

select *
from table 1
where if @Type='History'
then 
id=145
else if @type='latest'
then
code=452 and typeCode <> 3

or how to use a case with the above

where case @type='History'
then
id=145
when @type='Latest'
then
code=452 and typeCode <> 3

i can not get the syntax correct,how do i achieve this?

Aucun commentaire:

Enregistrer un commentaire