如何创建一个返回逗号分隔值列表的函数?
我正在尝试创建一个函数,该函数根据表名和列名参数返回逗号分隔的值列表。
这个想法是动态构建动态 SQL 语句,然后返回结果。但是,我遇到了一个问题,它不允许我在函数内部运行 execute
语句。这就是我的情况:
ALTER FUNCTION fn_HearingAttendeesToCSV (@TableName varchar(100),
@ColumnName varchar(100))
RETURNS VARCHAR(MAX)
AS
BEGIN
declare @sql varchar(max)
select @sql =
'DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr + '', '' ,'''') + '
+ @ColumnName + ' FROM ' + @TableName + ' RETURN @listStr '
Exec (@sql)
END
GO
我很确定我走错了路。有人可以建议如何解决 execute
问题吗?或者有更好的方法来做到这一点?
I am trying to create a function that returns a comma separated list of values based on a table name and column name parameters.
The idea was to build a dynamic SQL statement on the fly and then return the result. However, I ran into a problem where it won't let me run the execute
statement inside of a function. Here is what I had:
ALTER FUNCTION fn_HearingAttendeesToCSV (@TableName varchar(100),
@ColumnName varchar(100))
RETURNS VARCHAR(MAX)
AS
BEGIN
declare @sql varchar(max)
select @sql =
'DECLARE @listStr VARCHAR(MAX)
SELECT @listStr = COALESCE(@listStr + '', '' ,'''') + '
+ @ColumnName + ' FROM ' + @TableName + ' RETURN @listStr '
Exec (@sql)
END
GO
I am pretty sure that I am going down the wrong road. Can someone suggest how to get around the execute
issue? Or a better way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不能在函数期间执行此操作。函数无法执行动态 SQL,因此使用任意表/列名称(非确定性)与 SQL Server 函数背道而驰。
You cannot do this in a function - period. Functions cannot execute dynamic SQL, so using an arbitrary table/column name (non-deterministic) flies in the face of SQL Server functions.
我认为您无法通过 T-SQL 来完成此操作,但您绝对可以通过用 C# 编写的 UDF 来完成同样的事情。代码如下:
为了使用 CLR UDF,您需要确保已在服务器上启用它们,如下所示:
您还需要编译为旧版本的 .NET 框架(我测试的是 2.0): SQL 2005 不允许 4.0 程序集。
即使您的 UDF 是用 C# 编写的,您仍然可以按照习惯的方式通过 T-SQL 访问它:
如果您需要支持“dbo”以外的架构,或者不需要纯 CSV,请修改为满足您的需求,但这应该可以让您实现 99% 的目标。也就是说,假设您是 DBA 或者您与他/她是足够好的朋友来启用 CLR。
I don't think you can do this via T-SQL, BUT you absolutely can accomplish the same thing via a UDF written in C#. Here's the code:
In order to use CLR UDFs, you need to be sure you've enabled them on the server like this:
You also need to compile to an older version of the .NET framework (2.0 is what I tested with) as SQL 2005 doesn't allow 4.0 assemblies.
Even though your UDF is written in C#, you still access it via T-SQL the same way you're used to:
If you need to support schemas other than 'dbo', or if you don't need pure CSV, modify to fit your needs but this should get you 99% of the way there. That is, assuming you're the DBA or you're good enough buddies with him/her to get CLR enabled.
您好,我认为他们关于函数的说法是正确的,但是您可以使用带有输出参数的 SP 来执行此操作。
检查此代码:
您可以使用此代码调用它:
祝你好运,
GJ
Hi I think they're right about functions, but you can use an SP with an output parameter to do this.
Check this code:
You can call it with this code:
Good luck,
GJ
您不能在函数中执行此操作,但可以在 PROCEDURE 中执行此操作。我已经这样做过很多次来构建动态 SQL 语句。
这就是要走的路。
You can't do this in a function but you can do it in a PROCEDURE. I've done that many times to build a dynamic SQL statement.
That's the way to go.