sql - 接收错误 "Select statements included within a function cannot return data to a client"

标签 sql sql-server function select

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

Msg 444, Level 16, State 2, Procedure JDE_GetWhereClause_test, Line 26
Select statements included within a function cannot return data to a client.

有什么想法吗?

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

最佳答案

尝试玩一些类似......的东西

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

选择“答案是:”+[dbo].[JDE_GetWhereClause_test]('whatever')

关于sql - 接收错误 "Select statements included within a function cannot return data to a client",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15960094/

相关文章:

PHP min() 但不为空

R- 在函数内部赋值

sql - 如何对四列表中的数据进行排序?

php - 我应该在这里使用什么样的 JOIN?

sql - 一列( bool 值)定义另一列( bool 值),反之亦然

sql - SQLite不为空

sql-server - 服务代理 - 跨数据库选择插入本地数据库?

sql - 存储过程的 SELECT WHERE 语句中的 CASE?

sql-server - 当列 "createdDate"从现在起经过 90 天时,如何更新具有数百万行的 SQL Server 表中的列?我们可以使用触发器吗?

javascript - 为什么我不能以及如何将函数的返回值分配给对象属性?