Sid Brooklyn
Sid Brooklyn

Reputation: 1042

SQL scalar valued function throwing "Invalid Object Name" error

My SQL scalar valued function is defined in the following code:

CREATE FUNCTION CtrAmount ( @Ctr_Id int ) 
  RETURNS MONEY 
  AS 
  BEGIN 
      DECLARE @CtrPrice MONEY 
        SELECT @CtrPrice = SUM(amount) 
          FROM Contracts 
        WHERE contract_id = @Ctr_Id 
      RETURN(@CtrPrice) 
  END 
GO 

SELECT * FROM CtrAmount(345) 
GO

But when it comes to the SELECT line, I am getting this error:

Msg 208, Level 16, State 3, Line 14
Invalid object name 'CtrAmount'.

Upvotes: 0

Views: 1105

Answers (1)

Devart
Devart

Reputation: 121912

Int(10) - unknown type

CREATE FUNCTION dbo.CtrAmount
(
    @Ctr_Id INT
)
RETURNS MONEY
AS
BEGIN
    RETURN (
        SELECT SUM(amount)
        FROM dbo.Contracts
        WHERE contract_id = @Ctr_Id
    )
END
GO

SELECT dbo.CtrAmount(345)
GO

Upvotes: 2

Related Questions