/****** Object: StoredProcedure [dbo].[GetCommonGroupByRegion] Script Date: 03/23/2017 17:31:18 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER proc [dbo].[GetCommonGroupByRegion] @CarCategory varchar(50)=null,---車型 @CreateTimeStart datetime=null, --入會開始時間 @CreateTimeEnd datetime=null, --入會結束時間 @AuthenticationTimeStart datetime=null, --認證開始時間 @AuthenticationTimeEnd datetime=null, --認證結束時間 @BuyTimeStart datetime=null, --購車開始時間 @BuyTimeEnd datetime=null --購車結束時間 as begin DECLARE @strPC VARCHAR(2000), @strAPP VARCHAR(2000), @strWX VARCHAR(2000), @strWAP VARCHAR(2000) ---------------------pc start SET @strPC = ( SELECT DISTINCT STUFF( ( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK ) WHERE qtype = 'pc' ORDER BY qudao FOR XML PATH('')), 1,4, '') AS jzhw FROM CreatedType t) set @strPC= left(@strPC,len(@strPC)-1) -------------------------pc end*********** ------------------------app start SET @strAPP = ( SELECT DISTINCT STUFF( ( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK ) WHERE qtype = 'app' ORDER BY qudao FOR XML PATH('')), 1,4, '') AS jzhw FROM CreatedType t) set @strAPP= left(@strAPP,len(@strAPP)-1) ------------------------- app end ******* ------------------------wx start SET @strWX = ( SELECT DISTINCT STUFF( ( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK ) WHERE qtype = 'wx' ORDER BY qudao FOR XML PATH('')), 1,4, '') AS jzhw FROM CreatedType t) set @strWX= left(@strWX,len(@strWX)-1) ------------------------- wx end ******* ------------------------wap start SET @strWAP = ( SELECT DISTINCT STUFF( ( SELECT ','''+ qudao+'''' FROM CreatedType WITH ( NOLOCK ) WHERE qtype = 'wap' ORDER BY qudao FOR XML PATH('')), 1,4, '') AS jzhw FROM CreatedType t) set @strWAP= left(@strWAP,len(@strWAP)-1) ------------------------- wap end ******* select c.CarCategory as '車型', d.Region as '區域', COUNT(case when a.CreatedPerson like '%D%' and len(a.CreatedPerson)=5 then a.Id else null end ) as '經銷商', COUNT(case when a.CreatedPerson in( @strPC) then a.Id else null end ) as '網站', COUNT(case when a.CreatedPerson in(@strAPP) then a.Id else null end ) as 'APP', COUNT(case when a.CreatedPerson in(@strWX) then a.Id else null end ) as '微信', COUNT(case when a.CreatedPerson in( @strWAP) then a.Id else null end ) as 'wap' from Membership a left join IF_Customer b on a.IdentityNumber=b.IdentityNumber left join IF_Car c on b.CustId=c.CustId left join CS_CarDealerShip d on c.DealerId=d.DealerId where a.IsDel=0 --case when @CreateTimeStart is not null then and a.CreateTime>@CreateTimeStart else null end --and ( (@CreateTimeStart is not null and @CreateTimeStart!='') and (a.CreateTime>@CreateTimeStart) ) --and ( (@CreateTimeEnd is not null and @CreateTimeEnd!='') and (a.CreateTime<=@CreateTimeEnd) ) and (a.CreateTime>=@CreateTimeStart or @CreateTimeStart is null) and (a.CreateTime<=@CreateTimeEnd or @CreateTimeEnd is null) --and ( (@AuthenticationTimeStart is not null and @AuthenticationTimeStart!='') and (a.AuthenticationTime>@AuthenticationTimeStart) ) --and ( (@AuthenticationTimeEnd is not null and @AuthenticationTimeEnd!='') and (a.AuthenticationTime<=@AuthenticationTimeEnd) ) and (a.AuthenticationTime>=@AuthenticationTimeStart or @AuthenticationTimeStart is null) and (a.AuthenticationTime<=@AuthenticationTimeEnd or @AuthenticationTimeEnd is null) --and ( (@CarCategory is not null and @CarCategory!='') and (c.CarCategory=@CarCategory) ) and (c.CarCategory=@CarCategory or @CarCategory is null ) --and ( (@BuyTimeStart is not null and @BuyTimeStart!='') and ( c.BuyTime>@BuyTimeStart) ) --and ( (@BuyTimeEnd is not null and @BuyTimeEnd!='') and ( c.BuyTime<=@BuyTimeEnd) ) and (c.BuyTime>=@BuyTimeStart or @BuyTimeStart is null) and (c.BuyTime<=@BuyTimeEnd or @BuyTimeEnd is null) and d.Region<>'' and d.Region<>'-' group by c.CarCategory, d.Region ; end
解析: and (c.BuyTime<=@BuyTimeEnd or @BuyTimeEnd is null)
首先 and 條件后面是括號,那就說明括號 是一個整體, 括號里面 用的 or 語法,也就說明只要任意一個條件滿足就ok, 當參數為空的時候,假如作為條件的字段是時間格式的,如果做大小比較肯定會失敗,這個時候后面的 or的條件滿足了,就把前面的給忽略了,其實這個時候就相當於 and 1=1 了,