2

请检查以下查询

DECLARE @status varchar(1)
DECLARE @code varchar(50)

Set @status = '0'

select id,code from MasterTable where 
('07/31/2012' between  StartDate and EndDate) and 
Case when @status = '0' and Status=@status then 1 end = 1 

此查询适用于我,但不适用于@Status=1

我需要像这样的查询

if @Status = '0'
    select id,code from MasterTable where 
   ('07/31/2012' between  StartDate and EndDate) and Status = @Status
   if @code <> '0' 
        select id,code from MasterTable where 
        ('07/31/2012' between  StartDate and EndDate) and Status = @Status and code =@code
else
     select id,code from MasterTable where 
   ('07/31/2012' between  StartDate and EndDate) 
      if @code <> '0' 
        select id,code from MasterTable where 
        ('07/31/2012' between  StartDate and EndDate) and Status = @Status and code =@code     

我怎样才能实现这个用例 when 声明?

4

1 回答 1

2

您可以通过稍微改写查询来简化查询:

select  id,code 
from    MasterTable 
where   '07/31/2012' between  StartDate and EndDate)
        and (@status <> '0' or Status = @status)
于 2012-09-05T11:55:06.017 回答