SQL游标有些打印不起作用

时间:2016-03-15 09:10:51

标签: sql sql-server-2008 cursor

我在SQL中创建了一个游标。我想要的是,

  1. 首先获得当天日期后6个月的员工卡,

  2. 然后我得到该雇员的统计

  3. 然后我通过一些计算来计算工作天数的数量。

  4. 为此,光标如下所示

    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
    

    在那个游标中,我通过PRINTING它的值测试数据,但是对于某些打印它不起作用。

    详情如下:

    NOT PRINTING LINE

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

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

    打印线

    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)
    

    我很困惑为什么有些人没有打印出来。任何帮助将不胜感激。

    我正在使用SQL Server 2008

1 个答案:

答案 0 :(得分:1)

将@Final_PaidDayLop的选择替换为:

           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