日期:2014-05-17 浏览次数:20591 次
create procedure [dbo].[SPHistoryReacrdNew]
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,
@year int,@month int,@day int
--定义游标查询当天的数据
declare Record_cursor cursor for (select UserName,SubmitCount from SMS_SubmitRecord where
CONVERT(varchar(8),SubmitDate,112)=CONVERT(varchar(8),GetDate(),112))
--打开游标
open Record_cursor
--拿到游标赋值
FETCH NEXT FROM Record_cursor INTO @UserName,@SubmitCount
--循环数据
while @@FETCH_STATUS=0 --循环读取
begin
select @CUSNAME=a2.CUSNAME from SSO_USER_INFO a1,SSO_CUSTOMERS a2
where a1.CUSID=a2.CUSID and a1.USERNAME=@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=@Username) b;
set @year=year(getdate());
set @month=month(getdate())
set @day=day(getdate())
set @UserSuccessCount=@SubmitCount-@UserErrorCount
--把提取到的数据添加到历史记录表
insert into SMS_HistoryRecord(HistoryYear, HistoryMonth, HistoryDay,
CustomerName, UserName, UserSubmitCount, UserSubmitSuccess,
UserSubmitError, SendStateSuceessCount, SendStateErrorCount,
SendStateNoBackCount, CMNumberCount, CUNumberCount, CTNumberCount)values
(@year,@month,@day,@CUSNAME,@Username,@SubmitCount,@UserSuccessCount,
@UserErrorCount,@successCount,@errorCount,@NotBackCount,@SuccessCM,@SuccessCU,@SuccessCT);
FETCH NEXT FROM Record_cursor into @UserName,@SubmitCount
end
--提交所有变更
COMMIT TRAN
CLOSE Record_cursor; --关闭游标
deallocate Record_cursor; --释放游标
SET NOCOUNT OFF --恢复设置