Error - The multi-part identifier "dbo.ComputeLowest" could not be bound.

  • Hi

      I have written below code in function . When i execute 

    select [dbo].[ComputeLowest] it gives above error

    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

  • jsshivalik - Friday, October 26, 2018 5:00 AM

    Hi

      I have written below code in function . When i execute 

    select [dbo].[ComputeLowest] it gives above error

    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

    You'll need the parenthesis in the query
    select dbo].ComputeLowest()
    Have you checked if the table [dbo].[test] and the function [dbo].[ComputeLowest] exists in the current database?
    😎

    Further, I suggest you convert the scalar function into a inline table valued function, something like this

    CREATE OR ALTER FUNCTION [dbo].[ComputeLowest] ()
    RETURNS TABLE
    AS
    RETURN
    WITH Budgeted(VAL) AS
    (
    SELECT
      SUM (T.Target) AS VAL
    FROM dbo.test T
    )
    ,Achieved(VAL) AS
    (
    SELECT
      SUM (T.Achievement) AS VAL
    FROM dbo.test T
    )
    SELECT
      (B.VAL - A.VAL) AS ActualPercentage
    FROM   Budgeted  B
    CROSS APPLY Achieved  A
    ;

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply