日期:2014-05-17 浏览次数:20562 次
create procedure SPHistoryReacrdAdd
as
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
--定义参数
declare @Username varchar(20),@SubmitCount int,
@UserSuccessCount int, @UserErrorCount int,
@CUSNAME varchar(32),@successCount int,
@errorCount int,@NotBackCount int,
@SuccessCM int,@SuccessCU int, @SuccessCT int,
@n int,@t int
--定义游标查询当天的数据
declare Record_cursor cursor for (select UserName from SMS_SubmitRecord where
CONVERT(varchar(8),SubmitDate,112)=CONVERT(varchar(8),GetDate(),112))
set @n=1
select @t=COUNT(*) from SMS_SubmitRecord where
CONVERT(varchar(8),SubmitDate,112)=CONVERT(varchar(8),GetDate(),112)
--打开游标
open Record_cursor
--拿到游标赋值
FETCH NEXT FROM Record_cursor INTO @UserName
--循环数据
while (@n<=@t)
begin
select @CUSNAME=a2.CUSNAME from SSO_USER_INFO a1,SSO_CUSTOMERS a2
where a1.CUSID=a2.CUSID and a1.USERNAME=@UserName;
select @SubmitCount=SubmitCount from SMS_SubmitRecord where
CONVERT(varchar(8),SubmitDate,112)=CONVERT(varchar(8),GetDate(),112) and UserName=@UserName;
select @UserSuccessCount=COUNT(*) from SMS_MT where USERID=@UserName;
select @UserErrorCount=NumberCount from dbo.SMS_RejectMessage where UserName=@UserName;
select @successCount=successCount,@errorCount=errorCount,@NotBackCount=NotBackCount,
@SuccessCM=SuccessCM,@SuccessCU=SuccessCU,@SuccessCT=SuccessCT from (select USERID,
SUM(case when REPORT_STAT='DELIVRD' then 1 else 0 end) successCount,
SUM(case when REPORT_STAT<>'DELIVRD' then 1 else 0 end) errorCount,
SUM(case when REPORT_STAT IS NULL then 1 else 0 end) NotBackCount,
SUM(case when MOBILE_NO_TYPE=1 AND REPORT_STAT='DELIVRD' THEN 1 ELSE 0 END) SuccessCM,
SUM(case when MOBILE_NO_TYPE=2 AND REPORT_STAT='DELIVRD' THEN 1 ELSE 0 END) SuccessCT,
SUM(case when MOBILE_NO_TYPE=3 AND REPORT_STAT='DELIVRD' THEN 1 ELSE 0 END) SuccessCU
from SMS_MT where
Convert(varchar(8),INTO_TIME,112)=Convert(varchar(8),Getdate(),112)
group by USERID having USERID='admin') b;
insert into SMS_HistoryRecord(HistoryYear, HistoryMonth, HistoryDay,
CustomerName, UserName, UserSubmitCount, UserSubmitSuccess,
UserSubmitError, SendStateSuceessCount, SendStateErrorCount,
SendStateNoBackCount, CMNumberCount, CUNumberCount, CTNumberCount)values
(year(getdate()),month(getdate()),day(getdate()),@CUSNAME,@Username,@SubmitCount,@UserSuccessCount,
@UserErrorCount,@successCount,@errorCount,@NotBackCount,@SuccessCM,@SuccessCU,@SuccessCT);
FETCH NEXT FROM Record_cursor into @UserName
end
--提交所有变更
COMMIT TRAN
CLOSE Record_cursor; --关闭游标
deallocate Record_cursor; --释放游标
SET NOCOUNT OFF --恢复设置