SQL Server 2008 - 无法在函数 MS SQL 中声明变量



我是SQL新手,试图在MS SQL 2008R2中创建函数,但不能在函数内声明变量。这段代码有什么问题?

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

我得到了这样的错误:

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.

你需要这样写,变量的数据类型缺失

DECLARE @Count int;

如果你声明@Count没有数据类型,你应该提供它。

DECLARE @Count int

变量@Count没有数据类型。使用:

Declare @Count int

不要忘记在函数

中添加RETURN关键字

最新更新