SQL使用存储过程和游标

时间:2014-07-02 16:04:00

标签: sql

我有一个sql数据库表,如下所示。

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

我想记录如下:

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

0 =签入 1 =结账

规定

如果有一个时钟在01-07-2014,09:00:08,他错过了,他在2014年7月2日18:00时钟,

他在小时08:55:11,2014-07-02 09:00:12,以及17:00:14小时的同一时钟。

如果有人可以帮助我,请。

   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 < @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 > @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

END

1 个答案:

答案 0 :(得分:0)

不确定你要在这里制作什么但是看看DATEDIFF功能;在这种情况下它可能会有所帮助。即:发现两次之间的差异;如果它们处于正确的时间范围内,请将它们一起添加。