Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
DECLARE @attendance_date DATETIME
SET @attendance_date = '2014-02-19'
WHILE (@attendance_date <='2014-02-20' )
    BEGIN
        SELECT  TM.EmployeeID, TM.FullName, @attendance_date as AbsenteeDate FROM TM_User TM
        where TM.is_active=1  and  TM.EmployeeID not in
                                         (SELECT EM2.EmployeeID from Employee_Attendance EM2 WHERE
                                         convert(varchar(15),EM2.CheckTime,105) = convert(varchar(15),@attendance_date,105))
    SET @attendance_date = DATEADD(day,1,@attendance_date)
    continue
IF @attendance_date = '2014-02-20'
BREAK;

END
Posted
Updated 25-Feb-14 18:42pm
v2

1 solution

Hi,

Here what I do not know what result you are getting and do not know what you want to make it as single ;-)
I have assumed that you are getting two selects and you want to combine it into single select statement.
For that You could assign it to table variable from there you could get it as single statement I believe.

Here is the query to do that
SQL
DECLARE @TempTable Table( 
EmpID INT,
EmpName VARCHAR(50),
AttendanceDate DATETIME
); 

DECLARE @attendance_date DATETIME
SET @attendance_date = '2014-02-19'
WHILE (@attendance_date <='2014-02-20' )
    BEGIN
        INSERT INTO @TempTable
        SELECT  TM.EmployeeID, TM.FullName, @attendance_date as AbsenteeDate FROM TM_User TM
        where TM.is_active=1  and  TM.EmployeeID not in
                                         (SELECT EM2.EmployeeID from Employee_Attendance EM2 WHERE
                                         convert(varchar(15),EM2.CheckTime,105) = convert(varchar(15),@attendance_date,105))
    SET @attendance_date = DATEADD(day,1,@attendance_date)
    continue
IF @attendance_date = '2014-02-20'
BREAK;
END
 SELECT * FROM @TempTable --Here you will get the result I believe

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
Member 10501509 26-Feb-14 3:19am    
i am getting these errors
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near '('.
Msg 1087, Level 15, State 2, Line 12
Must declare the table variable "@TempTable".
Msg 1087, Level 15, State 2, Line 22
Must declare the table variable "@TempTable".
♥…ЯҠ…♥ 26-Feb-14 5:09am    
Updated my solution, now try and lemme know
Member 10501509 26-Feb-14 23:11pm    
Thank you

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