title: SQL using stored procedure and cursor
body: <p>I have a sql database table as follows.</p>
  
  <pre><code>SeqID DevicesID EmployeeID Type Time
  1     2         624        0    2014-07-01 09:00:08.000
  2     2         624        1    2014-07-02 01:00:18.000
  3     2         624        0    2014-07-02 08:55:11.000
  4     2         624        0    2014-07-02 09:00:12.000
  5     2         624        1    2014-07-02 17:00:14.000
  </code></pre>
  
  <p>I want to take the record is as follows:</p>
  
  <pre><code>SeqID DevicesID EmployeeID Type Time
  1     2         624        0    2014-07-01 09:00:08.000
  2     2         624        1    2014-07-02 01:00:18.000
  3     2         624        0    2014-07-02 08:55:11.000
  5     2         624        1    2014-07-02 17:00:14.000
  </code></pre>
  
  <p>0 = Check In
  1 = Check Out</p>
  
  <p>provisions</p>
  
  <p>if there is a clock in on 01-07-2014, 09:00:08 hours he missed, and he clocked 1:00:18 hours on 02-07-2014,</p>
  
  <p>he was on the same clock in hours 08:55:11, 2014-07-02 09:00:12, and 17:00:14 hours clocked on.</p>
  
  <p>If anyone could help me, please. </p>
  
  <pre><code>   CREATE PROCEDURE [dbo].[SP_SynchronizeCheckInOutBackup]
     @DevicesID AS INTEGER,
     @EmployeeID AS INTEGER,
     @Type AS INTEGER,
     @Time AS DATETIME
     AS
     BEGIN
          DECLARE @SequenceID AS INTEGER
          DECLARE @TempTime AS DATETIME
  
          IF (@Type = 0)
          BEGIN
               IF NOT EXISTS (SELECT * FROM CheckInOut WHERE DevicesID = @DevicesID AND 
                  EmployeeID = @EmployeeID AND Type = @Type)
                  BEGIN
                      INSERT INTO CheckInOut(DevicesID, EmployeeID, Type, Time)
                      VALUES (@DevicesID, @EmployeeID, @Type, @Time)
                  END
               ELSE
                  BEGIN
                      SELECT @TempTime = Time FROM CheckInOut 
                      WHERE DevicesID = @DevicesID AND EmployeeID = @EmployeeID AND Type 
                      = @Type
  
                      IF (@Time &lt; @TempTime) 
                           BEGIN
                                SELECT @SequenceID = SequenceID FROM CheckInOut 
                                WHERE DevicesID = @DevicesID AND EmployeeID = 
                                @EmployeeID AND Type = @Type 
  
                              UPDATE CheckInOut
                  SET Type = @Type, Time = @Time 
                  WHERE SequenceID = @SequenceID
              END 
          END
  END
  
  IF (@Type = 1)
  BEGIN
      IF NOT EXISTS (SELECT * FROM CheckInOut WHERE DevicesID = @DevicesID AND EmployeeID = @EmployeeID AND Type = @Type)
          BEGIN
              INSERT INTO CheckInOut(DevicesID, EmployeeID, Type, Time)
              VALUES (@DevicesID, @EmployeeID, @Type, @Time)
          END
      ELSE
          BEGIN
              SELECT @TempTime = Time FROM CheckInOut 
              WHERE DevicesID = @DevicesID AND EmployeeID = @EmployeeID AND Type = @Type
  
              IF (@Time &gt; @TempTime) 
              BEGIN
                  SELECT @SequenceID = SequenceID FROM CheckInOut 
                  WHERE DevicesID = @DevicesID AND EmployeeID = @EmployeeID AND Type = @Type 
  
                  UPDATE CheckInOut
                  SET Type = @Type, Time = @Time 
                  WHERE SequenceID = @SequenceID
              END 
          END
  END
  </code></pre>
  
  <p>END</p>