标题:新手,求助SQL 2000 触发器问题
取消只看楼主
虎子8989
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2015-11-2
结帖率:0
 问题点数:0 回复次数:0 
新手,求助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,以后触发后导致日记录数据不能记录
大家帮我分析分析是什么原因
搜索更多相关主题的帖子: 数据库表 触发器 记录 
2016-10-16 17:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-469863-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.163471 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved