Select 包含在函数中的语句不能 return 数据到 client_New

Select statements included within a function cannot return data to a client_New

我一直在阅读问题标题中的错误,但我无法弄清楚下面代码中的错误是什么:

CREATE function [RPT].[MonthlyRollupDates_Delimited]()
RETURNS @table TABLE (Value varchar(max))
AS
BEGIN

Declare @Count int , @Date date = getdate(),@Month int
SET @Month = MONTH(@Date)
SET  @Count = @Month +12


Select 
top (@Count) 
CalendarDate,   BusinessDay, Label
from  MonthlyRollupDates
order by Calendardate desc

return
end
GO

我得到的错误是

Select statements included within a function cannot return data to a client

你需要把你SELECT的结果放入你的returntable,然后return那个table。

CREATE function [RPT].[MonthlyRollupDates_Delimited]()
--notice i modified the table to match the select statement, but assumed datatypes
RETURNS @table TABLE (CalendarDate datetime, BusinessDay int, Label char(10))
AS
BEGIN

declare @Count int
SET @Count = month(getdate()) + 12

--place your select into the table variable
insert into @table
Select top (@Count) 
    CalendarDate
    ,BusinessDay
    ,Label
from  
    MonthlyRollupDates
order by 
    Calendardate desc

return
end
GO