sql server CLR标量函数,默认列名

Ada*_*ski 7 c# t-sql clr user-defined-functions sql-server-2008

我想知道,如果有任何方法可以在Sql Server中为我的CLR标量函数添加列名.我的意思是,在我运行查询之后,我希望看到一个带有此函数结果的列已经用自定义而不是自定义命名(No column name).

我知道通常将函数组合在一起,或者由于其他原因我必须将它们命名为不同的东西,但仍然 - 当您编写一个包含30列的查询时,不必为其中的20列打入别名就不错了.

那么有人知道一个能够实现此目的的黑客攻击吗?

通过SSMS中的一些插件来获得此功能也很好(例如,从计算中使用的函数和列构建虚拟别名,例如"datediff_startdate_enddate").我试图找到一个现成的解决方案,但没有效果.任何提示?

编辑:有人问我关于代码示例.我不认为这会有多大帮助,但这里是:

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlTypes;
using System.Text.RegularExpressions;

namespace ClrFunctions
{
    public class RegexFunctions
    {

        public static SqlBoolean clrIsMatch(SqlString strInput, SqlString strPattern)
        {
            if (strPattern.IsNull || strInput.IsNull)
            {
                return SqlBoolean.False;
            }
            return (SqlBoolean)Regex.IsMatch(strInput.Value, strPattern.Value, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

T-SQL:

Create Assembly ClrFunctions From 'C:\CLR\ClrFunctions.dll' 
GO

Create Function dbo.clrIsMatch(
    @strInput As nvarchar(4000),
    @strPattern As nvarchar(255)
)
Returns Bit
As External Name [ClrFunctions].[ClrFunctions.RegexFunctions].[clrIsMatch]
GO
Run Code Online (Sandbox Code Playgroud)

这是我希望在T-SQL中调用此函数的预期结果:

select 
    txt, dbo.clrIsMatch(txt,'[0-9]+') 
from (
    select 'some 123 text' as txt union all 
    select 'no numbers here' union all 
    select 'numbers 456 again'
) x
Run Code Online (Sandbox Code Playgroud)

结果已经有一个列名,无需在T-SQL中添加别名: 在此输入图像描述

fno*_*tro 2

我假设你在某个地方调用了这样的函数:

command.CommandText = "SELECT SomeFunction();";
Run Code Online (Sandbox Code Playgroud)

尝试这个

command.CommandText = "SELECT SomeFunction() 'AliasForColumnNameYouWant';";
Run Code Online (Sandbox Code Playgroud)

不确定 20 个或 30 个函数是什么意思?

您是说在 Sql-Server 内有一些存储过程或函数调用 20 或 30 个不同的标量函数吗?

或者同一个函数可能被调用 20 或 30 次?

您是否会将结果合并到可返回的数据行或数据列中?

请需要代码示例。