【问题标题】:Select statements included within a function cannot return data to a client_New函数中包含的 Select 语句无法将数据返回到 client_New
【发布时间】:2019-05-07 05:04:18
【问题描述】:

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

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 语句不能将数据返回到 客户

【问题讨论】:

  • 您使用的是哪个 dbms? (MS SQL Server?)
  • 我正在使用 Microsoft SQL Server Management Studio

标签: sql-server tsql stored-functions


【解决方案1】:

您需要将SELECT的结果放入您的返回表中,然后返回该表。

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

【讨论】:

  • 我不知道我需要将列名和数据类型放在 "Return @table" 之后,我将根据获取它们的表检查正确的数据类型并更新脚本如果需要的话。谢谢,我会在测试后将这个问题标记为已解决:)
  • 对不起,我几分钟前就可以测试了,商业项目和大学作业太多了:(..谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-04
  • 1970-01-01
  • 1970-01-01
  • 2014-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多