SQL Server 2008 批量插入数据时触发器脚本实例

--SQL Server 2008 批量插入数据时,触发器脚本实例
ALTER TRIGGER [dbo].[T_Name]
   ON  [dbo].[HT_CurrentTagRecord]
   AFTER INSERT
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    --INSERT
    BEGIN     
        declare @I_Code nvarchar(20);
        declare @I_CreateTime datetime;
        declare @I_WorkState int;
        
        declare MyCursor cursor for
        SELECT Code, CreateTime, WorkState FROM INSERTED 
        open MyCursor;
        fetch next from MyCursor into @I_Code, @I_CreateTime, @I_WorkState; --不判断@I_CreateTime是否大于当前的StartTime了,因为批量发过来的数据中都是一个标签一条数据
        while @@fetch_status=0    --循环读取
        begin
            declare @Id int;        
            declare @Code nvarchar(20);
            declare @StartTime datetime;
            declare @StopTime datetime;
            
            if(exists(SELECT 1 FROM DBName.dbo.StaticsTable S WHERE Code = @I_Code))
            begin
                SELECT top 1 @Id = Id, @Code = S.Code, @StartTime = S.StartTime, @StopTime = S.StopTime  --查询StaticsTable中最新的记录
                FROM DBName.dbo.StaticsTable S
                WHERE Code = @I_Code
                order by S.Id desc;
            
                --分析:
                --如果最新的记录的终止统计时间(即StopTime)为空,说明该时间段设备还没关机
                --如果最新的记录的终止统计时间(即StopTime)不为空,说明该时间段设备已关机
                
                --如果是新增的记录是 "关机" 数据记录
                if(@I_WorkState = 0)
                begin
                    if(ISNULL(@StopTime,'') = '')  --如果最新的记录的终止统计时间为空,说明该时间段的统计还没结束,则让其终止
                    begin
                        update DBName.dbo.StaticsTable set StopTime=@I_CreateTime,UseMinuteTime = DATEDIFF(n, @StartTime, @I_CreateTime) where Id = @Id;
                    end
                end
                else if(@I_WorkState = 1) --如果是新增的记录是 "开机" 数据记录
                begin
                    if(ISNULL(@StopTime,'') = '')  --如果最新的记录的终止统计时间为空,说明该时间段的统计还没结束,则刷新该记录的使用时长
                    begin
                        if(ISNULL(@StartTime,'') = '')
                        begin
                            update DBName.dbo.StaticsTable set StartTime = @I_CreateTime, UseMinuteTime = 0 where Id = @Id;
                        end
                        else
                        begin
                            update DBName.dbo.StaticsTable set UseMinuteTime = DATEDIFF(n, @StartTime, @I_CreateTime) where Id = @Id;
                        end
                    end
                    else --如果最新的记录的终止时间不为空,表示该时间的统计已终止,则创建一条新的统计记录
                    begin
                        Insert into DBName.dbo.StaticsTable values(@I_Code, @I_CreateTime, null, 0);
                    end
                end
            end
            else
            begin --如果StaticsTable表中未找到相应的记录,则插入一条初始记录
                Insert into DBName.dbo.StaticsTable values(@I_Code, @I_CreateTime, null, 0);
            end
            
            fetch next from MyCursor into @I_Code, @I_CreateTime, @I_WorkState;
        end;
        close MyCursor;
        deallocate MyCursor;
    END
END

 

上一篇:SqlServer之快速查看表结构(表描述及字段说明)


下一篇:SQL数据库字段添加说明文字