USE [WTCS]
GO
/****** Object: StoredProcedure [dbo].[sp_Week] Script Date: 01/28/2010 14:13:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: deepwishly
-- Create date: 2010.1.15
-- Description: 周数据统计
-- =============================================
ALTER procedure [dbo].[sp_Week]
as
begin
--声明变量部分
declare @todayDate datetime
declare @lastWeekStartDate datetime
declare @temp varchar(50) --用于存放游标值的中间变量
declare @num int --记录上周是否有数据,大于零,说明抄表器已经将周数据,发送过来,就不需要计算了
declare @count int --记录临时表中是否有数据
--赋值部分
set @todayDate = (select Convert(varchar(100),GETDATE(),23))
set @lastWeekStartDate = (select DATEADD(day,-7,@todayDate))
--如果存在,删除临时表
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempActualDate') and type='U')
begin
drop table #tempActualDate
end
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#tempCircleTable') and type='U')
begin
drop table #tempCircleTable
end
--创建二个临时表,
select distinct Number into #tempCircleTable from equipment_ActualData -- 用于游标循环
select * into #tempActualDate from equipment_ActualData --用于操作提供操作的临时表
begin tran
declare tempCircleTable_Cursor cursor for select Number from #tempCircleTable
open tempCircleTable_Cursor
while(@@FETCH_STATUS=0) --返回被fetch语句执行的最后游标的状态
begin
fetch next from tempCircleTable_Cursor into @temp
--如果存在临时表,将其删除
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_WeekTable') and type='U')
begin
drop table #temp_WeekTable
end
if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#temp_temp') and type='U')
begin
drop table #temp_temp
end
--创建一个临时表用于存储游标等于@temp的一周数据
select * into #temp_WeekTable from #tempActualDate where Number =@temp and
datediff(d,ReceiveDate,@lastWeekStartDate)<=0 order by receiveDate asc
select * into #temp_temp from #temp_WeekTable order by receiveDate desc
--记录临时表中是否有数据
set @count = (select count(*) from #temp_WeekTable)
--声明用于存放一周数据起始与结束二条数据变量
--存放起始日期的
declare @ReceiveDate datetime
declare @Equipment_type nvarchar(100)
declare @Number int
declare @EngineHour_S float
declare @HoistHour_S float
declare @TrolleryHour_S float
declare @GantryHour_S float
declare @HTHour_S float
declare @Twistlock_S float
declare @Bak1_H_S float
declare @Bak2_H_S float
declare @Bak3_H_S float
declare @Bak1_D_S float
--声明一周结束日期的
declare @EngineHour_E float
declare @HoistHour_E float
declare @GantryHour_E float
declare @HTHour_E float
declare @TrolleryHour_E float
declare @Twistlock_E float
declare @Bak1_H_E float
declare @Bak2_H_E float
declare @Bak3_H_E float
declare @Bak1_D_E float
--声明插入周数据表中的变量
declare @EngineHour_Sub float
declare @HoistHour_Sub float
declare @TrolleryHour_Sub float
declare @GantryHour_Sub float
declare @HTHour_Sub float
declare @Twistlock_Sub float
declare @Bak1_H_Sub float
declare @Bak2_H_Sub float
declare @Bak3_H_Sub float
declare @Bak1_D_Sub float
--为各变量赋值
set @ReceiveDate =(select top 1 ReceiveDate from #temp_WeekTable )
set @Equipment_type =(select top 1 Equipment_type from #temp_WeekTable )
set @Number =(select top 1 Number from #temp_WeekTable )
set @EngineHour_S =(select top 1 EngineHour from #temp_WeekTable )
set @HoistHour_S =(select top 1 HoistHour from #temp_WeekTable )
set @TrolleryHour_S = (select top 1 TrolleryHour from #temp_WeekTable)
set @GantryHour_S =(select top 1 GantryHour from #temp_WeekTable )
set @HTHour_S =(select top 1 HTHour from #temp_WeekTable )
set @Twistlock_S =(select top 1 Twistlock from #temp_WeekTable )
set @Bak1_H_S =(select top 1 Bak1_H from #temp_WeekTable )
set @Bak2_H_S =(select top 1 Bak2_H from #temp_WeekTable )
set @Bak3_H_S =(select top 1 Bak3_H from #temp_WeekTable )
set @Bak1_D_S =(select top 1 Bak1_D from #temp_WeekTable )
set @EngineHour_E =(select top 1 EngineHour from #temp_temp )
set @HoistHour_E=(select top 1 HoistHour from #temp_temp )
set @TrolleryHour_E = (select top 1 TrolleryHour from #temp_temp)
set @GantryHour_E =(select top 1 GantryHour from #temp_temp )
set @HTHour_E =(select top 1 HTHour from #temp_temp )
set @Twistlock_E =(select top 1 Twistlock from #temp_temp )
set @Bak1_H_E =(select top 1 Bak1_H from #temp_temp )
set @Bak2_H_E =(select top 1 Bak2_H from #temp_temp )
set @Bak3_H_E =(select top 1 Bak3_H from #temp_temp )
set @Bak1_D_E =(select top 1 Bak1_D from #temp_temp )
--整理待插入周数据表数据
set @EngineHour_Sub =(@EngineHour_E-@EngineHour_S)
set @HoistHour_Sub =(@HoistHour_E-@HoistHour_S)
set @TrolleryHour_Sub =(@TrolleryHour_E-@TrolleryHour_S)
set @GantryHour_Sub =(@GantryHour_E-@GantryHour_S)
set @HTHour_Sub =(@HoistHour_E-@HoistHour_S)
set @Twistlock_Sub =(@Twistlock_E-@Twistlock_S)
set @Bak1_H_Sub =(@Bak1_H_E-@Bak1_H_S)
set @Bak2_H_Sub =(@Bak2_H_E-@Bak2_H_S)
set @Bak3_H_Sub =(@Bak3_H_E-@Bak3_H_S)
set @Bak1_D_Sub =(@Bak1_D_E-@Bak1_D_S)
set @num =(select (select count(*) from equipment_WeekData where datediff(d,WeekDate,@ReceiveDate)>=0 and Equipment_type=@Equipment_type and Number=@Number ))
if(@num>0 )
begin
continue
end
else if(@count>0)
begin
--将数据插入到周数据表
insert into equipment_WeekData(WeekDate,Equipment_type,Number,EngineHour_WD,HoistHour_WD,TrolleryHour_WD,GantryHour_WD,
HTHour_WD,Twistlock_WD,Bak1_H_WD,Bak2_H_WD,Bak3_H_WD,Bak1_D_WD) VALUES( @ReceiveDate,@Equipment_type,@Number,@EngineHour_Sub,@HoistHour_Sub,@TrolleryHour_Sub,
@GantryHour_Sub,@HTHour_Sub,@Twistlock_Sub,@Bak1_H_Sub,@Bak2_H_Sub,@Bak3_H_Sub,@Bak1_D_Sub)
set @num=0
end
if(@@error<>0)
begin
print('rollback tran')
rollback tran
return 0
end
commit tran
--数据库邮件部分
declare @info varchar(100)
if(@count=0)
begin
set @info =(convert(varchar(100),getdate(),120)+'实时数据表中没有上周可用实时数据')
end
else
begin
set @info =(convert(varchar(100),getdate(),120)+'周数据存储过程执行成功')
end
exec msdb.dbo.sp_send_dbmail
@recipients='wangsl@donetech.com.cn',
@subject ='数据库操作提醒',
@body = @info,
@profile_name ='DBEmail_Config';
end
close tempCircleTable_Cursor --关闭游标
deallocate tempCircleTable_Cursor --释放游标
end