如果员工迟到或早退或忘记打卡或退房,请显示备注

时间:2016-06-08 07:33:46

标签: sql-server stored-procedures

CREATE PROCEDURE usp_EmployeeAttendance (
    @Emp_No nvarchar(50),
    @In_Time_Punch datetime,
    @Out_Time_Punch datetime,
    @Remarks nvarchar(50), @Status varchar(10),
    @Att_Date date, @Hrs nchar(10), @W_Days int
)
AS

BEGIN
    INSERT INTO Emp_Att (Emp_No, In_Time_Punch, Out_Time_Punch, Hrs, Status, Remarks, W_Days, Att_Date)
    VALUES
    (
        @Emp_No,
        @In_Time_Punch,
        @Out_Time_Punch,
        @Hrs,
        @Status,
        @Remarks,
        @W_Days,
        @Att_Date
    )

    IF (@In_Time_Punch > '7:25:00')
    BEGIN
        SET @Remarks = 'LC'
    END
    ELSE
    IF (@In_Time_Punch = '')
    BEGIN
        SET @Remarks = 'IN PUNCH FORGET'
        IF (@In_Time_Punch < '16:35:00')
        BEGIN
            SET @Remarks = 'EL'
        END
        ELSE
        IF (@Out_Time_Punch = '')
        BEGIN
            SET @Remarks = 'OUT PUNCH FORGET'
        END
    END
END

1 个答案:

答案 0 :(得分:0)

您可以使用VALUES语法,而不是在INSERT语句中使用INSERT...SELECT。使用Remarks表达式设置CASE的值:

INSERT INTO Emp_Att (Emp_No, In_Time_Punch, Out_Time_Punch, Hrs, Status, Remarks, W_Days, Att_Date)
SELECT
    @Emp_No,
    @In_Time_Punch,
    @Out_Time_Punch,
    @Hrs,
    @Status,
    CASE
        WHEN @In_Time_Punch > '7:25:00' THEN 'LC'
        WHEN @In_Time_Punch = '' THEN
            CASE
                WHEN @In_Time_Punch < '16:35:00' THEN 'EL'
                WHEN @Out_Time_Punch = '' THEN 'OUT PUNCH FORGET'
                ELSE 'IN PUNCH FORGET'
            END
    END,
    @W_Days,
    @Att_Date