locked
SQL cursor some print is not working RRS feed

  • Question

  • User-1506965535 posted

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

    • First get the Employee card no 6 months back of today's date then,
    • Then I get the count of that employee
    • 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)         -- Not working
    
    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. Any help would be appreciated.

    I am using SQL server 2008

    Tuesday, March 15, 2016 9:19 AM

Answers

  • User632428103 posted

    Hello nadeem 157,

    before, check your data => if your data is NULL the message  will be equal to null !

    try this :

    PRINT 'this message is invisible' + null

    last month i have the same problem i've search for several day ...

    if that doesn't solve your problem, comment some line and check all from the begining ...

    hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2016 10:01 AM
  • User-1506965535 posted

    hey jimmy69,

    thanks for your reply. I tried that too,

    But finally got the answer from below link

    http://stackoverflow.com/questions/36006833/sql-cursor-some-print-is-not-working

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2016 10:29 AM

All replies

  • User632428103 posted

    Hello nadeem 157,

    before, check your data => if your data is NULL the message  will be equal to null !

    try this :

    PRINT 'this message is invisible' + null

    last month i have the same problem i've search for several day ...

    if that doesn't solve your problem, comment some line and check all from the begining ...

    hope this help

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2016 10:01 AM
  • User-1506965535 posted

    hey jimmy69,

    thanks for your reply. I tried that too,

    But finally got the answer from below link

    http://stackoverflow.com/questions/36006833/sql-cursor-some-print-is-not-working

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 15, 2016 10:29 AM