我有了一個可空參數的存儲過程,而下面是我的查詢如何編寫有多個case的where子句?
select *
from table1
where table1.id = isnull(@id, table1.id)
現在也有一些特殊的ID我區別對待。我加入另一個表表2像下面
combid id
1 abc01
1 abc02
1 abc03
2 hig01
2 hig02
我不得不更改查詢以滿足下列情況
- 如果
@id
爲null,where子句將table1.id = table1.id
- 如果
@id
不爲空,
2.1如果@id
存在於表2中,where子句將是table1.id in (select id from table2 where combid in (select combid from table2 where [email protected]))
2.2否則where子句將是table1.id = @id
我試過下面的查詢,但不起作用。
select * from table1
where ([email protected] and not exists(select * from table2 where [email protected])
or @id is null
or table1.id in (select id from table2 where combid in (select combid where [email protected])) and exists(select * from table2 where [email protected]))
如何更改存儲過程的查詢?
[到底什麼,你試過嗎?(http://whathaveyoutried.com) – 2013-03-28 16:28:11
是的,你嘗試過什麼? – Leng
可能是http://stackoverflow.com/questions/7403301/multiple-conditions-in-where-clause將幫助你。 –