6

I'm newbie in SQL and trying to create function in MS SQL 2008R2, but can't declare variable inside function. What's wrong with this code?

CREATE FUNCTION denominator() RETURNS int
BEGIN
    DECLARE @Count;
    -- Some logic here
END;

GO
SELECT dbo.denominator()

DROP FUNCTION denominator

I'm getting that kind of errors:

Msg 102, Level 15, State 1, Procedure denominator, Line 3
Incorrect syntax near ';'.
Msg 4121, Level 16, State 1, Line 1
Cannot find either column "dbo" or the user-defined function or aggregate "dbo.denominator", or the name is ambiguous.
1
  • Why you aren't using postgres or mysql dbs? I think it gives huge profit for you. Commented Oct 28, 2013 at 14:11

3 Answers 3

10

you need to write like this , data type of variable is missing

DECLARE @Count int;
Sign up to request clarification or add additional context in comments.

Comments

2

you're declaring of @Count has no data type, you should provide it.

DECLARE @Count int

Comments

1

The variable @Count doe not have a data type.Use this:

Declare @Count int

Don't forget to add the RETURN keyword in the function

Comments

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.