接收错误“选择函数中包含的语句不能将数据返回给客户端”

时间:2013-04-11 22:38:00

标签: sql sql-server function select

尝试在函数中使用Select语句时收到错误。错误说明:

  

Msg 444,Level 16,State 2,Procedure JDE_GetWhereClause_test,Line 26
  函数中包含的Select语句不能将数据返回给客户端。

有什么想法吗?

CREATE FUNCTION [dbo].[JDE_GetWhereClause_test]
(
@tablename as varchar
)
RETURNS varchar(max)
AS
BEGIN
-- Declare the return variable here
Declare @ResultVar as varchar(max)

-- Add the T-SQL statements to compute the return value here

set @tablename = 'F0101'
Declare @Sql nvarchar(max)
Declare my_cur cursor for
    SELECT fsuser FROM dbo.JDE_ExRowSecurity where fsuser = fsuser;

Declare @fsuser as nchar(15)
open my_cur;
fetch next from my_cur;
while @@fetch_status = 0
   begin
      fetch next from my_cur into @fsuser;    
      set @ResultVar += ',' + @fsuser;
   end;
close my_cur;
deallocate my_cur;

-- Return the result of the function
RETURN @ResultVar
END

2 个答案:

答案 0 :(得分:12)

尝试玩像......

CREATE FUNCTION [dbo].[JDE_GetWhereClause_test]
(
@tablename as varchar
)
RETURNS varchar(max)
AS
BEGIN
  -- Declare the return variable here
  Declare @ResultVar as varchar(max)

  -- Add the T-SQL statements to compute the return value here

  set @ResultVar = (select STUFF((SELECT ',', fsuser as [text()]
                    FROM dbo.JDE_ExRowSecurity 
                    FOR XML PATH ('')), 1, 1, '') as blah)

  -- Return the result of the function
  RETURN @ResultVar
END

select 'Answer is: '+[dbo].[JDE_GetWhereClause_test]('whatever')

答案 1 :(得分:0)

发现此MSDN article有用,它解决了我的问题:

RETURN (SELECT GETDATE());

直接从select语句返回值,避免变量。