SQL Server : 选择"Value or All"



我有一个表T1,列ID1, ID2

我需要写一个像这样的"选择"存储过程

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
select * from your_table
where (@id1 = 'All' or ID1 =  @id1)
  and (@id2 = 'All' or ID2 =  @id2)

相关内容

最新更新