Unable to declare a variable in MS SQL function

I am new to SQL and trying to create a function in MS SQL 2008R2, but I can not declare a variable inside the function. What is wrong with this code?

CREATE FUNCTION denominator() RETURNS int BEGIN DECLARE @Count; -- Some logic here END; GO SELECT dbo.denominator() DROP FUNCTION denominator 

I get errors like this:

 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. 
+4
source share
3 answers

you need to write like this: there is no variable data type

 DECLARE @Count int; 
+9
source

you declare that @Count does not have a data type, you must provide it.

 DECLARE @Count int 
+1
source

There is no data type in the @Count variable. Use this:

 Declare @Count int 

Remember to add the RETURN keyword in the function

0
source

All Articles