SQL Server:选择“值或全部”

时间:2015-08-30 19:12:27

标签: sql-server

我的表格T1包含ID1ID2列。

我需要像这样编写一个“选择”存储过程

Create SP_GetT1ByID1AndID2
    @id1 varchar(50), @id2 varchar(50)
as
begin
    Select * 
    from T1 
    where 
         // If (ID1 = "All" and  ID2 = "All") so we have to return all the data in the table T1
         //  If (ID1 = "All" and ID2 != "All") so we have filter the data according to ID2 
         //  If (ID1 != "All" and ID2 = "All") so we have filter the data according to ID1
         //  If (ID1 != "All" and ID2 != "All") so we have filter the data according to ID1 and ID2
end

1 个答案:

答案 0 :(得分:0)

select * from your_table
where (@id1 = 'All' or ID1 =  @id1)
  and (@id2 = 'All' or ID2 =  @id2)