为什么我的扳机不起作用?

时间:2017-05-12 13:36:43

标签: sql sql-server-2008

我有这个触发器,没有任何错误地创建。但它不会在插入时触发

CREATE TRIGGER dbo.Get_Each_InOut
ON [SiPassAPI].[dbo].[INFO]
AFTER INSERT
AS
BEGIN
    IF EXISTS(SELECT eventno FROM INSERTED 
              WHERE message LIKE '%valid%' AND message LIKE '%entry%' 
                AND type != 0)
    BEGIN
         INSERT INTO [SiPassAPI].[dbo].[Daily_Atten_Events] (emp_id, empid, date_occurred, working_date, in_time, out_time, duration_present)    
             SELECT 
                 i.emp_id, e.emp_no, date_occurred,
                 SUBSTRING(i.date_occurred, 1, 4) + '-' +
                 SUBSTRING(i.date_occurred, 5, 2) + '-' + 
                 SUBSTRING(i.date_occurred, 7, 2),
                 SUBSTRING(i.date_occurred, 1, 4) + '-' + 
                 SUBSTRING(i.date_occurred, 5, 2) + '-' +
                 SUBSTRING(i.date_occurred, 7, 2) + ' '+
                 SUBSTRING(i.time_occurred, 1, 2) + ':' + 
                 SUBSTRING(i.time_occurred, 3, 2) + ':' +
                 SUBSTRING(i.time_occurred, 5, 2),
                 '', '' 
             FROM 
                 INSERTED i 
             INNER JOIN
                 [asco4].[asco].[employee] e ON e.emp_id = i.emp_id;
   END;

   IF EXISTS (SELECT eventno FROM INSERTED 
              WHERE message LIKE '%valid%' AND message LIKE '%exit%' 
              AND type != 0)
   BEGIN
         DECLARE @time2 varchar(20);
         DECLARE @date_val varchar(20);
         DECLARE @time_val varchar(20); 
         DECLARE @emp_id varchar(20); 

         SELECT
             @emp_id = emp_id,
             @date_val = date_occurred,
             @time_val = time_occurred 
         FROM 
             INSERTED   

         SET @time2 = SUBSTRING(@date_val,1,4)+'-'+SUBSTRING(@date_val,5,2)+'-'+SUBSTRING(@date_val,7,2)+' '+SUBSTRING(@time_val,1,2)+':'+SUBSTRING(@time_val,3,2)+':'+SUBSTRING(@time_val,5,2);
         UPDATE [SiPassAPI].[dbo].[Daily_Atten_Events] 
         SET out_time = @time2,
             duration_present = CONVERT(varchar(10), DATEDIFF(HOUR, in_time, @time2)) + ':' +
                                CONVERT(varchar(10), DATEDIFF(MI, in_time, @time2) % 60)
         WHERE 
             emp_id = @emp_id AND out_time = NULL
   END;
END;
GO

Daily_Atten_Events中没有插入任何记录。

如何排查此触发器?

0 个答案:

没有答案