扩大范围的多条件查询

代码
declare @TempTab Table(id int,a varchar(50),b varchar(50),c varchar(100));
insert into @TempTab(id,a,b,c) values(1,'aaa111','bbb111','ccc111');
insert into @TempTab(id,a,b,c) values(2,'aaa222','bbb222','ccc222');
insert into @TempTab(id,a,b,c) values(3,'aaa333','bbb333','ccc333');
insert into @TempTab(id,a,b,c) values(4,'aaa444','bbb444','ccc444');
insert into @TempTab(id,a,b,c) values(5,'aaa555','bbb555','ccc555');
select * from @TempTab;

declare @q_a varchar(50);
declare @q_b varchar(50);
declare @q_c varchar(50);
set @q_a='aaa111';
set @q_b='bbb222';
set @q_c='ccc111';
--set @q_a='aaa111';
--
set @q_b='bbb222';
--
set @q_c='ccc333';

declare @count int
select @count=count(*from @TempTab where a=@q_a and b=@q_b and c=@q_c
if @count>0
    
begin
        
select * from @TempTab where a=@q_a and b=@q_b and c=@q_c
    
end
else
    
begin
        
select @count=count(*from @TempTab where (a=@q_a and b=@q_bor (a=@q_a and c=@q_cor (b=@q_b and c=@q_c)
        
if @count>0
            
begin
                
select * from @TempTab where (a=@q_a and b=@q_bor (a=@q_a and c=@q_cor (b=@q_b and c=@q_c)
            
end
        
else
            
begin
                
select @count=count(*from @TempTab where a=@q_a or b=@q_b or c=@q_c
                
if @count>0
                    
begin
                        
select * from @TempTab where a=@q_a or b=@q_b or c=@q_c
                    
end
                
else
                    
begin
                        
select * from @TempTab
                    
end
            
end
    
end
代码
declare @orgcity varchar(200);
declare @dstcity varchar(200);
declare @FromTime DATETIME;
declare @flightNo varchar(50);
declare @AirCompanyCode varchar(50);
declare @CabinType varchar(50);

set @orgcity='CAN';
set @dstcity='XIY';
set @FromTime='2010-05-08 00:00:00:000';
set @flightNo='cz3208';
set @AirCompanyCode='cz';
set @CabinType='u';


SELECT * FROM SpecialManager where 
(
@FromTime between Orgdate and DstDate and orgcity like '%'+@orgcity+'%' and dstcity like '%'+@dstcity+'%' and specialType=0)

SELECT  top 1 * FROM SpecialManager where (@FromTime between Orgdate and DstDate and orgcity like '%'+@orgcity+'%' and dstcity like '%'+@dstcity+'%' and specialType=0and (flightNo=@flightNo  OR ISNULL(flightNo,'')=''AND (AirCompanyCode=@AirCompanyCode  OR ISNULL(AirCompanyCode,'')=''AND (CabinType like '%'+@CabinType+'%'  OR ISNULL(CabinType,'')=''ORDER BY AirCompanyCode desc,flightNo desc,CabinType desc
原文地址:https://www.cnblogs.com/xnxylf/p/1732235.html