namco
namco

Reputation: 6338

Why my simple T-Sql Function can not run in Query WIndow?

I created a function in SQL Server 2012 Express with the code below.

CREATE FUNCTION IncAge(@Age AS INT)
RETURNS INT
AS
BEGIN
    DECLARE @VAR AS INT;
    SET @VAR = @Age + 10;
    RETURN @VAR;
END

And when I try to call this function from query window, I get an error.

With SELECT IncAge(20) I get error

'IncAge' is not a recognized built-in function name.

With IncAge(20) I get error

Incorrect syntax near '20'.

So what is the problem??

Upvotes: 1

Views: 60

Answers (1)

marc_s
marc_s

Reputation: 754240

Function have to always be referenced with their schema:

Try

SELECT dbo.IncAge(20) 

It's a generally good idea to always use the schema qualifier (usually dbo. - see Bad Habits to Kick: avoiding the schema prefix) for everything - tables, views, stored procedures - but in the case of functions, they're mandatory

Upvotes: 1

Related Questions