Sql server 2005 试图编译函数时语法不正确
我正在尝试创建以下函数:Sql server 2005 试图编译函数时语法不正确,sql-server-2005,user-defined-functions,Sql Server 2005,User Defined Functions,我正在尝试创建以下函数: CREATE FUNCTION fn_WarrantyTrend returns @myTable table ( Years int ) AS BEGIN insert into @myTable SELECT distinct YEAR(CurDate) from EOD_Main ORDER BY YEAR(CurDate) ASC RETURN END 我得到了这些错误: Msg 102,15级,状态1,程序fn_保修趋势,
CREATE FUNCTION fn_WarrantyTrend
returns @myTable table
(
Years int
)
AS
BEGIN
insert into @myTable
SELECT distinct YEAR(CurDate) from EOD_Main ORDER BY YEAR(CurDate) ASC
RETURN
END
我得到了这些错误:
Msg 102,15级,状态1,程序fn_保修趋势,第2行
“returns”附近的语法不正确。
Msg 1087,15级,状态2,程序fn_保修趋势,第8行
必须声明表变量“@myTable”
当您从编写的语法中得到错误时,您应该: 不过,有趣的是,您使用这个
@Years
参数,却从不使用它
您还应该考虑使用StudiabIng< <代码>创建函数<代码>。和
CREATE FUNCTION dbo.fn_WarrantyTrend -- always use schema prefix
(
@Years int -- need @variable syntax here
)
-- parameter list comes *before* RETURNS
RETURNS TABLE -- you should use an inline table-valued function when possible
AS
RETURN
(
SELECT DISTINCT CurDate = YEAR(CurDate) -- should alias the output column
FROM dbo.EOD_Main -- again, always use SCHEMA prefix
--ORDER BY YEAR(CurDate) ASC -- not possible to ORDER BY here without TOP
-- the outer query that calls the function should apply any ORDER BY
);