#2
mywisdom882016-10-17 13:43
|
数据库表日记录和月记录,表格式如下:日记录:
只有本站会员才能查看附件,请 登录
;月记录:
只有本站会员才能查看附件,请 登录
在日记录中建立如下触发器:
CREATE TRIGGER [dbo].[TriggerAddDayData]
ON dbo.日记录
AFTER INSERT,UPdate
AS
BEGIN
SET NOCOUNT ON;
declare @tt datetime
declare @COD_Min real
declare @COD_Avg real
declare @COD_Max real
declare @NH3_Min real
declare @NH3_Avg real
declare @NH3_Max real
declare @PH_Min real
declare @PH_Avg real
declare @PH_Max real
declare @XFW_Min real
declare @XFW_Avg real
declare @XFW_Max real
declare @LiuLiang_Min real
declare @LiuLiang_Avg real
declare @LiuLiang_Max real
declare @LJLL_Min real
declare @LJLL_Avg real
declare @LJLL_Max real
declare @Flag bit
declare @count int
declare @ncount real
if((select count(*) from inserted)!=0)
select @tt=时间,@Flag=状态标识 from inserted
select @COD_Min = avg(COD_Min),@COD_Avg=avg(COD_Avg),@COD_Max=avg(COD_Max),@NH3_Min = avg(氨氮_Min),@NH3_Avg=avg(氨氮_Avg),@NH3_Max=avg(氨氮_Max),@PH_Min = avg(PH_Min),@PH_Avg=avg(PH_Avg),@PH_Max=avg(PH_Max),@XFW_Min = avg(悬浮物_Min),@XFW_Avg=avg(悬浮物_Avg),@XFW_Max=avg(悬浮物_Max),@LiuLiang_Min=avg(流量_Min),@LiuLiang_Avg=avg(流量_Avg),@LiuLiang_Max=avg(流量_Max),@LJLL_Min=sum(累计流量_Min),@LJLL_Avg=sum(累计流量_Avg),@LJLL_Max=sum(累计流量_Max)
from dbo.日记录 where year(时间)=year(@tt) and month(时间)=month(@tt) and 状态标识=8
if exists(select * from 月记录 where year(时间)=year(@tt) and month(时间)=month(@tt)) begin select @ncount=count(*) from 日记录 where year(时间)=year(@tt) and month(时间)=month(@tt) and 状态标识=8
if @ncount>=24
begin
update dbo.月记录
set
COD_Min=@COD_Min,
COD_Avg=@COD_Avg,
COD_Max=@COD_Max,
氨氮_Min=@NH3_Min,
氨氮_Avg=@NH3_Avg,
氨氮_Max=@NH3_Max,
PH_Min=@PH_Min,
PH_Avg=@PH_Avg,
PH_Max=@PH_Max,
悬浮物_Min=@XFW_Min,
悬浮物_Avg=@XFW_Avg,
悬浮物_Max=@XFW_Max,
流量_Min=@LiuLiang_Min,
流量_Avg=@LiuLiang_Avg,
流量_Max=@LiuLiang_Max,
累计流量_Min=@LJLL_Min,
累计流量_Avg=@LJLL_Avg,
累计流量_Max=@LJLL_Max,
状态标识=8,
count=@ncount,
标志=0
where year(时间)=year(@tt) and month(时间)=month(@tt)
end
else
update dbo.月记录
set
COD_Min=@COD_Min,
COD_Avg=@COD_Avg,
COD_Max=@COD_Max,
氨氮_Min=@NH3_Min,
氨氮_Avg=@NH3_Avg,
氨氮_Max=@NH3_Max,
PH_Min=@PH_Min,
PH_Avg=@PH_Avg,
PH_Max=@PH_Max,
悬浮物_Min=@XFW_Min,
悬浮物_Avg=@XFW_Avg,
悬浮物_Max=@XFW_Max,
流量_Min=@LiuLiang_Min,
流量_Avg=@LiuLiang_Avg,
流量_Max=@LiuLiang_Max,
累计流量_Min=@LJLL_Min,
累计流量_Avg=@LJLL_Avg,
累计流量_Max=@LJLL_Max,
状态标识=9,
count=@ncount,
标志=0
where year(时间)=year(@tt) and month(时间)=month(@tt)
end
else
begin
insert into dbo.月记录([时间],[COD_Min],[COD_Avg],[COD_Max],[氨氮_Min],[氨氮_Avg],[氨氮_Max],[PH_Min],[PH_Avg],[PH_Max],[悬浮物_Min],[悬浮物_Avg],[悬浮物_Max],[流量_Min],[流量_Avg],[流量_Max],[累计流量_Min],[累计流量_Avg],[累计流量_Max],[状态标识],[标志])
values
(@tt,@COD_Min,@COD_Avg,@COD_Max,@NH3_Min,@NH3_Avg,@NH3_Max,@PH_Min,@PH_Avg,@PH_Max,@XFW_Min,@XFW_Avg,@XFW_Max,@LiuLiang_Min,@LiuLiang_Avg,@LiuLiang_Max,@LJLL_Min,@LJLL_Avg,@LJLL_Max,9,0)
end
END
以上触发器第一次触发后,月数据表中的数据为NULL,以后触发后导致日记录数据不能记录
大家帮我分析分析是什么原因