locked
How to Debug RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below function and want to debug or print values of @Achieved & @Budgeted

    ALTER FUNCTION [dbo].[ComputeLowest] ()
    RETURNS DECIMAL(18,2)
     BEGIN
    	DECLARE @ActualPercentage DEC (18,2) , @Budgeted DEC (18,2),  @Achieved DEC (18,2)
        SELECT @Budgeted = SUM (Target) FROM [test] 
    	SELECT @Achieved = SUM (Achievement) FROM [test] 
    
    
    	SET @ActualPercentage = @Budgeted -  @Achieved
    	
     RETURN @ActualPercentage
    End

    Thanks
    Friday, October 26, 2018 11:31 AM

Answers

  • User475983607 posted

    Correct can't use PRINT in a function. 

    But why do you need a function in the first place as the function does not take inputs and therefore the result is always based on the entire tables.   Just copy the code into SSMS and run...

    DECLARE @ActualPercentage DEC (18,2) , @Budgeted DEC (18,2),  @Achieved DEC (18,2)
    SELECT @Budgeted = SUM (Target) FROM [test] 
    SELECT @Achieved = SUM (Achievement) FROM [test] 
    
    PRINT @Budgeted
    PRINT @Achieved 
    
    SET @ActualPercentage = @Budgeted -  @Achieved

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 4:22 PM

All replies

  • User475983607 posted

    Simply...

    PRINT @Achieved 
    PRINT @Budgeted

    Friday, October 26, 2018 1:08 PM
  • User-1499457942 posted

    Hi

     It is giving error - Invalid use of a side-effecting operator 'PRINT' within a function

    Thanks

    Friday, October 26, 2018 3:59 PM
  • User475983607 posted

    Correct can't use PRINT in a function. 

    But why do you need a function in the first place as the function does not take inputs and therefore the result is always based on the entire tables.   Just copy the code into SSMS and run...

    DECLARE @ActualPercentage DEC (18,2) , @Budgeted DEC (18,2),  @Achieved DEC (18,2)
    SELECT @Budgeted = SUM (Target) FROM [test] 
    SELECT @Achieved = SUM (Achievement) FROM [test] 
    
    PRINT @Budgeted
    PRINT @Achieved 
    
    SET @ActualPercentage = @Budgeted -  @Achieved

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 26, 2018 4:22 PM
  • User753101303 posted

    Hi,

    Is this really needed ? You could just show :

      SELECT SUM (Target),SUM(Achievement) FROM [test] 
    

    Not sure what is your issue what the result you call a "percentage" is actually a "difference" ? You want to subtract or divide the value ?

    Friday, October 26, 2018 4:45 PM