Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm trying to create a trigger which allow me to save data after insert from another table "Emp_AttendanceByDevice"
My table Emp_AttendanceByDevice is like this:

EnrollNumber    InOutMode   wYear   wMonth   wDay    wHour   wMinute   wSecond
27                0          2015     7       9        8       0          0
27                2          2015     7       9        10      0          0
27                3          2015     7       9        11      0          0
27                1          2015     7       9        12      0          0
27                0          2015     7       9        13      0          0
27                1          2015     7       9        17      0          0  

InOutMode--> 0: CheckIn 1: CheckOut 2: BreakIn 3: BreakOut

I want to get a trigger save data like the example below:

EnrollNumber       InOutMode       DateAttendance       TimesInOut
27                   0              07-09-2015           07-09-2015 08:00:00
27                   2              07-09-2015           07-09-2015 10:00:00
27                   3              07-09-2015           07-09-2015 11:00:00
27                   1              07-09-2015           07-09-2015 12:00:00
27                   0              07-09-2015           07-09-2015 13:00:00
27                   1              07-09-2015           07-09-2015 17:00:00

I tried the query below but It gives me redundant values!
SQL
Create TRIGGER [dbo].[Trigger_Attendance] 
   ON  [dbo].[emp_AttendanceByDevice] 
   AFTER INSERT
AS 
BEGIN
   
      INSERT INTO Log_Attendance(EnrollNumber, DateAttendance,TimesInOut,Mode)
      SELECT 
       emp_AttendanceByDevice.EnrollNumber,
         CAST(	cast(wYear as char(4))+'-'++cast(wMonth as varchar(2))+'-'++cast(wDay as varchar(2)) as datetime),
       CAST(	
         cast(wYear as char(4))+'-'++cast(wMonth as varchar(2))+'-'++cast(wDay as varchar(2))+' '+
         
         cast(wHour as char(4))+':'++cast(wMinute as varchar(2))+':'++cast(wSecond as varchar(2)) as datetime),

 emp_AttendanceByDevice.InOutMode
      FROM emp_AttendanceByDevice
END

Any solution please?
Posted

1 solution

Instead of selecting the data to insert from the actual table use special inserted and deleted tables.

For more information, see Use the inserted and deleted Tables[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900