注册 登录
编程论坛 SQL Server论坛

新手,求助SQL 2000 触发器问题

虎子8989 发布于 2016-10-16 17:48, 2681 次点击
新手,求助SQL 2000 触发器问题
数据库表日记录和月记录,表格式如下:日记录:
 
只有本站会员才能查看附件,请 登录

;月记录:
只有本站会员才能查看附件,请 登录


 在日记录中建立如下触发器:
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,以后触发后导致日记录数据不能记录
 大家帮我分析分析是什么原因
2 回复
#2
mywisdom882016-10-17 13:43
以上触发器第一次触发后,月数据表中的数据为NULL,以后触发后导致日记录数据不能记录!
是不是你数据中有NULL,由于NULL不能和其他参数运算的,可能会导致你的语句无法运行,存在NULL的字段作为查询条件时,要注意判断是否为NULL
另外,你语句很长,别人不好检查,建议你自己减少字段数量,自己分步测试,然后逐渐增加字段数量。这样比较容易找出问题出在那里。

[此贴子已经被作者于2016-10-17 13:45编辑过]

#3
qingcong332017-02-16 16:56
1