Search code examples
sqlsql-server-2008cursor

SQL cursor some print are not being output


I have created a cursor in SQL. What I want is,

  1. First get the Employee card no 6 months back of today's date then,

  2. Then I get the count of that employee

  3. Then I calculate the the no of days worked and all by some calculation.

For that the cursor is something like below

IF(@Type = 'C') 
    BEGIN
            Print 'Yes I am in the Current process';        
        DECLARE daily_Allocate CURSOR FOR  
                Select distinct c.emp_card_no, c.emp_name, c.Dt_Of_Join from emp_mst c  
                    join emp_mon_day d 
                    on c.emp_card_no=d.emp_mkey 
                    WHERE Dt_Of_Join =  CAST(FLOOR(CAST( DATEADD(month, -6, GETDATE()) AS FLOAT ))AS DATETIME)  
            OPEN daily_Allocate   
            FETCH NEXT FROM daily_Allocate INTO
            @emp_card_no, @emp_name, @Dt_Of_Join
            WHILE @@FETCH_STATUS = 0   
            BEGIN   
                    select  @Total_days = Sum(total_day),@Days_worked = Sum(days_worked)
                    from emp_mon_day a      
                    where  a.emp_mkey = @emp_card_no
                    group by emp_mkey       
                    
                        PRINT 'Employee Card no ' + cast(@emp_card_no as char)      --Working
                        PRINT 'Total days ' + cast(@Total_days as char)             --Working
                        PRINT 'Days Worked ' +  cast(@Days_worked as char)          --Working
                        
                set @Final_PaidDayLop = 0;
                declare @TotalRecord as varchar(155)
                set @TotalRecord = 0;
            
                Select @Final_PaidDayLop = sum(days)       
                    from  P_Emp_Del_App_Hdr c join P_Emp_Del_App_trl d on c.mkey=d.mkey       
                    where c.delete_flag='N' and app_flag='Y' and c.year = @actualYear
                and c.emp_mkey = @emp_card_no
                
                PRINT 'Final Paid LOP '  + cast(@Final_PaidDayLop as char)         -- Not working
    
            Select @TotalRecord =  ((1.75 * 6) / @Total_days) * (@Days_worked + @Final_PaidDayLop) 
                from emp_mon_day a where  a.emp_mkey = @emp_card_no group by emp_mkey   
    
            PRINT 'Total Record ' + cast(@TotalRecord as char)                     -- Not working
            
                  FETCH NEXT FROM daily_Allocate INTO 
                  @emp_card_no, @emp_name, @Dt_Of_Join 
            END   
        CLOSE daily_Allocate   
        DEALLOCATE daily_Allocate
END

In that cursor, I'm testing the data by PRINTING its value, but for some print it is not working.

DETAILS are mentioned below

NOT PRINTING LINE

PRINT 'Final Paid LOP '  + cast(@Final_PaidDayLop as char)

AND

PRINT 'Total Record ' + cast(@TotalRecord as char)

PRINTING LINE

PRINT 'Employee Card no ' + cast(@emp_card_no as char)
                        PRINT 'Total days ' + cast(@Total_days as char)
                        PRINT 'Days Worked ' +  cast(@Days_worked as char)

I'm confused why some are not getting printed.

I am using SQL Server 2008.


Solution

  • Replace the select of @Final_PaidDayLop with this:

               Select @Final_PaidDayLop = coalesce(sum(days),0)      
                    from  P_Emp_Del_App_Hdr c join P_Emp_Del_App_trl d on c.mkey=d.mkey       
                    where c.delete_flag='N' and app_flag='Y' and c.year = @actualYear
                and c.emp_mkey = @emp_card_no