日期:2014-05-17  浏览次数:20904 次

存储过程参数为空时where的条件不参与筛选
如下 @strAgentid int, @strGroupid int,2个参数,可能传递过来的时候为'',这样查询的结果就为空了。

请问老大门,怎么解决存储过程参数的判断,就跟C#里if如果为空就跳过的意思??

USE [ykCommSuites]
GO
/****** 对象: StoredProcedure [dbo].[getInboundCallDetail] 脚本日期: 08/23/2012 09:01:40 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER procedure [dbo].[getInboundCallDetail]
(
  @p_begintime varchar(20),
  @p_endtime varchar(20),
  @strAgentid int,
  @strGroupid int,
  @QueryType int
)
as

begin
select A.CALLINGNUMBER,convert(varchar(10),A.BEGINTIME,120) iDate,convert(varchar(10),A.BEGINTIME,108) iBegintime,
case 
when TYPE%10=2 then '呼到路由点' when TYPE%10=1 then '呼到IVR' when TYPE%10=3 then 'IVR转路由点' ELSE '---'
end IVRORROUT,
case 
when TYPE%10=1 and TYPE <> 411 then '请求IVR转移' when TYPE = 411 then 'IVR自助服务' else '---' 
end IVRTRANORSELF,
case 
when TYPE>100 and TYPE<=399 then convert(char(10),A.ENQUEUETIME,108) else '---' 
end ENTERQUEUETIME,
case 
when TYPE>100 and TYPE<=399 then A.ROUTEPOINT else '---' 
end ROUTEPOINT,
case 
when TYPE>100 and TYPE<=399 then convert(char(10),dateadd(second,A.QUEUEDURATION,A.ENQUEUETIME),108) else '---' 
end ENDQUEUETIME,
case 
when TYPE>=200 and TYPE<=399 then '转到坐席' when TYPE>100 and TYPE<200 then '客户放弃' else '---' 
end ENDQUEUECAUSE,
case 
when TYPE between 300 and 399 then '接听' when TYPE between 200 and 299 then '未接' else '---'  
end AGENTISANSWER,
case 
when TYPE between 200 and 399 then convert(char(10),B.BEGINTIME,108) else '---'  
end BEGINAGENTTIME,
case 
when TYPE between 300 and 399 then convert(char(10),dateadd(second,B.RINGDURATION,B.BEGINTIME),108) else '---'  
end PICKUPTIME,
case 
when TYPE between 200 and 399 then convert(char(10),dateadd(second,B.RINGDURATION+B.TALKDURATION,B.BEGINTIME),108) else '---'  
end ENDAGENTTIME,
case 
when TYPE between 200 and 399 then B.AGENTID else '---'  
end AGENTID,
case 
when TYPE between 200 and 399 then B.MONITORPARTY else '---'  
end MONITORPARTY,GROUPID
from (
select * 
from REPORT_T_ALLCALL_CDR 
where BEGINTIME>=cast(@p_begintime as datetime) and BEGINTIME<=cast(@p_endtime as datetime) and TYPE>100
and
(
(@QueryType=9) or
  (@QueryType=0 and TYPE%10=2) or
  (@QueryType=1 and TYPE%10=1) or
(@QueryType=2 and TYPE%10=3) or
(@QueryType=3 and TYPE=411) or
(@QueryType=4 and TYPE%10=1 and TYPE <> 411) or
(@QueryType=5 and TYPE>100 and TYPE<200) or
(@QueryType=6 and TYPE>=200 and TYPE<=399) or
(@QueryType=7 and TYPE between 300 and 399) or
(@QueryType=8 and TYPE between 200 and 299)
)
)A left join 
(
select R.*,C.GROUPID 
from REPORT_T_AGENT_CDR R left outer join CTI_T_AGENTINGROUP C on R.AGENTID=C.AGENTID
where CATEGORY=0 
and datediff(day, convert(datetime, '01/01/2008', 101), cast(@p_begintime as datetime))<=beginday
and datediff(day, convert(datetime, '01/01/2008', 101), cast(@p_endtime as datetime))>=beginday 
)B 
on A.enumcallid=B.enumcallid and agentid = @strAgentid and groupid =@strGroupid  
order by idate desc,iBegintime asc
end

------