1

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??

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Just for completeness they don't always need to be DECLARE @Result INT; EXEC @Result = IncAge 20;SELECT @Result works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.