编写MS SQL Server触发器的正确​​方法

时间:2019-01-30 15:22:22

标签: sql-server datetime

这是我第一次在MS SQL上进行编码,我想确保使用的结构正确。

我正在编写一个插入触发器,它将根据前一个记录的值更新当前记录的几个值。 Cum_cnt字段具有累积计数,它可以在某些点重设,因此如果else语句捕获重设则添加(当值小于前一个值时)。我正在计算记录之间的计数并从以前的记录更新开始时间。

根据我的其他数据库知识,我已经安装了MS SQL数据库并编写了以下触发器。它可以按预期工作,但是我不确定这是否是编写方法,如果我们更新7-8个字段,会不会出现性能问题。

create table t1 (dt datetime, cum_cnt int, cnt int, st_time datetime, ed_time datetime) 
--drop trigger t1_Trg
CREATE   TRIGGER t1_Trg 
ON  t1
AFTER INSERT 
AS  
DECLARE @v_prev_cnt int
DECLARE @v_curr_cnt int   
DECLARE @v_prev_dt datetime 
DECLARE @v_curr_dt datetime 
DECLARE @v_cnt int
   BEGIN 
    select @v_curr_cnt = i.cum_cnt, @v_curr_dt = i.dt from inserted i

    select @v_prev_cnt = cum_cnt, @v_prev_dt = dt from t1  where dt = (select max(dt) from t1 where dt < @v_curr_dt)
    if (@v_curr_cnt >= @v_prev_cnt)
        set @v_cnt =  @v_curr_cnt - @v_prev_cnt;
    else
        set @v_cnt =  @v_curr_cnt;
    UPDATE t1
    SET st_time = @v_prev_dt,
        ed_time = @v_curr_dt,
        cnt = @v_cnt
    FROM t1
    where dt = @v_curr_dt
END  

--delete from t1

insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:01:00.000' ), 10, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:05:00.000' ), 23, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:10:00.000' ), 27, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:13:00.000' ), 32, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:15:00.000' ), 33, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:17:00.000' ), 33, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:19:00.000' ), 41, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:20:00.000' ), 43, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:23:00.000' ), 04, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:26:00.000' ), 08, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:31:00.000' ), 11, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:34:00.000' ), 15, NULL, NULL, NULL);
insert into t1 (dt , cum_cnt , cnt , st_time , ed_time ) values (Convert(datetime, '2019-01-29 13:35:00.000' ), 18, NULL, NULL, NULL);

结果:

select * from t1

dt                 cum_cnt cnt  st_time                 ed_time
2019-01-29 13:01:00.000 10  10  NULL                    2019-01-29 13:01:00.000
2019-01-29 13:05:00.000 23  13  2019-01-29 13:01:00.000 2019-01-29 13:05:00.000
2019-01-29 13:10:00.000 27  4   2019-01-29 13:05:00.000 2019-01-29 13:10:00.000
2019-01-29 13:13:00.000 32  5   2019-01-29 13:10:00.000 2019-01-29 13:13:00.000
2019-01-29 13:15:00.000 33  1   2019-01-29 13:13:00.000 2019-01-29 13:15:00.000
2019-01-29 13:17:00.000 33  0   2019-01-29 13:15:00.000 2019-01-29 13:17:00.000
2019-01-29 13:19:00.000 41  8   2019-01-29 13:17:00.000 2019-01-29 13:19:00.000
2019-01-29 13:20:00.000 43  2   2019-01-29 13:19:00.000 2019-01-29 13:20:00.000
2019-01-29 13:23:00.000 4   4   2019-01-29 13:20:00.000 2019-01-29 13:23:00.000
2019-01-29 13:26:00.000 8   4   2019-01-29 13:23:00.000 2019-01-29 13:26:00.000
2019-01-29 13:31:00.000 11  3   2019-01-29 13:26:00.000 2019-01-29 13:31:00.000
2019-01-29 13:34:00.000 15  4   2019-01-29 13:31:00.000 2019-01-29 13:34:00.000
2019-01-29 13:35:00.000 18  3   2019-01-29 13:34:00.000 2019-01-29 13:35:00.000

0 个答案:

没有答案