触发器无法按预期的方式计算累计运行时间

时间:2019-11-26 16:23:13

标签: sql-server triggers

我有一个表能源和一个触发器表运行时间。当任何记录插入的数据应在触发表中更新时,在插入触发器时应根据反应堆关闭时间增加关闭或关闭持续时间(基于React_off_Durartion = off / on.total on time列对于插入的第一个值而言不正确)。 对于第一个记录,不应添加。

触发

USE [EnergyMonitoringDB]
GO
/****** Object:  Trigger [dbo].[Energies_INSERT]    Script Date: 11/26/2019 9:43:49 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 ALTER TRIGGER [dbo].[Energies_INSERT]
   ON  [dbo].[Energies]
   AFTER  INSERT
AS 
BEGIN

 If (SELECT reactor_off_Durartion FROM INSERTED) ='on'
 Begin
 UPDATE run_hours
   SET  latest_status = reactor_off_Durartion,last_modified=recorddatetime,
    total_ontime += (SELECT DATEDIFF(second,(select last_modified from run_hours),RecordDateTime))
   FROM inserted
    End
   Else
    UPDATE run_hours
   SET  latest_status = reactor_off_Durartion,last_modified=recorddatetime,
   total_offtime += (SELECT DATEDIFF(second,(select last_modified from run_hours),RecordDateTime))
   FROM inserted
   end

触发表

total_ontime    total_offtime   latest_status   last_modified   status
-2563200    0   on  2019-11-26 09:00:00.000 0

total_ontime    int 
total_offtime   int 
latest_status   varchar(10) 
last_modified   datetime    
status  int 



 Energies table




     Id y_status    RecordDateTime  DeviceId    reactor_off_Durartion   b_current   r_current   y_current   b_status    r_status
6071    1   2019-11-26 09:00:00.0000000 1   on  1   1   1   1   1

0 个答案:

没有答案