locked
Why is function call returning 'Invalid object name'? RRS feed

  • Question

  • It should be something simple, but I can't seem to see it.
    Why is this SELECT statement returning Invalid object name 'dbo.ufn_SalesByStore'?
    CREATE FUNCTION dbo.ufn_SalesByStore (@storeid int)
    RETURNS INT
    AS
    BEGIN
    RETURN (20)
    END
    GO
    --SELECT Call returning 'Invalid object name 'dbo.ufn_SalesByStore'
    SELECT * FROM dbo.ufn_SalesByStore (602);
    Thanks.
    Wednesday, November 30, 2011 2:48 PM

Answers

  • Hi,

    This is a scalar funtion.so we should use the return value in side select statement.you are trying to use the function in place of a table,that is why it is throwing error.

    Please refer to the link below on Executing User-Defined Functions 

    http://msdn.microsoft.com/en-us/library/ms175562.aspx


    Vinay Valeti| If you think my suggestion is useful, please rate it as helpful. If it has helped you to resolve the problem, please Mark it as Answer
    • Proposed as answer by Naomi N Thursday, December 1, 2011 2:11 AM
    • Marked as answer by KJian_ Wednesday, December 7, 2011 3:36 AM
    Wednesday, November 30, 2011 3:06 PM

All replies

  • Try:

    SELECT dbo.ufn_SalesByStore (602);
    


    Regards,

    Asim Bagwan

    Kindly mark the replies as Answers if they help!

    • Proposed as answer by Naomi N Thursday, December 1, 2011 2:11 AM
    Wednesday, November 30, 2011 2:54 PM
  • Hello,

    You should use as

    SELECT dbo.ufn_SalesByStore (602);
    

     

    Your function is a scalar function.

    If it was a table valued function, then you can use FROM keyword to read returned data from function

     


    SQL Server, SQL Server 2012 Denali and T-SQL Tutorials
    • Proposed as answer by Naomi N Thursday, December 1, 2011 2:11 AM
    Wednesday, November 30, 2011 2:55 PM
  • Because you created a Scalar Function, not a Table-Valued Function.
    • Proposed as answer by Adam_Turner Wednesday, November 30, 2011 3:14 PM
    Wednesday, November 30, 2011 2:57 PM
  • Hi,

    This is a scalar funtion.so we should use the return value in side select statement.you are trying to use the function in place of a table,that is why it is throwing error.

    Please refer to the link below on Executing User-Defined Functions 

    http://msdn.microsoft.com/en-us/library/ms175562.aspx


    Vinay Valeti| If you think my suggestion is useful, please rate it as helpful. If it has helped you to resolve the problem, please Mark it as Answer
    • Proposed as answer by Naomi N Thursday, December 1, 2011 2:11 AM
    • Marked as answer by KJian_ Wednesday, December 7, 2011 3:36 AM
    Wednesday, November 30, 2011 3:06 PM