【问题标题】:Can I develop an SQL/CLR function that can accept arguments of any type like MAX()?我可以开发一个 SQL/CLR 函数来接受任何类型的参数,如 MAX() 吗?
【发布时间】:2012-08-20 12:51:45
【问题描述】:

例如,我可以自己使用 C# 开发 MAX() 函数吗?谢谢!

【问题讨论】:

  • 你能详细说明一下吗?我不清楚你在问什么。

标签: sql-server sqlclr


【解决方案1】:

如本文所述 (http://stackoverflow.com/questions/4749071/clr-table-valued-function-with-array-argument?rq=1),不支持表值参数。 SQL/CLR 函数接受命名空间 System.Data.SqlTypes 中类型的参数 - 例如 SqlChars、SqlString、SqlInt32 等。基本上,它们是原始类型:不允许行集、不允许作为函数参数的数组(上一个链接中的答案提出变通办法)。

SQL/CLR 函数更适合利用 System.Text.RegularExpressions 命名空间 (Regex.Match) 或创建返回给定值(可能是密码字符串)哈希值的函数。

以下是 CLR 程序集中的 SQL 函数示例:

public static partial class UserDefinedFunctions
{
    public static readonly RegexOptions Options = RegexOptions.IgnorePatternWhitespace | RegexOptions.Singleline;

    [SqlFunction]
    public static SqlBoolean RegexMatch(SqlChars input, SqlString pattern)
    {
        Regex regex = new Regex(pattern.Value, Options);
        return regex.IsMatch(new string(input.Value));
    }

    [SqlFunction]
    public static SqlChars RegexGroup(SqlChars input, SqlString pattern, SqlString name)
    {
        Regex regex = new Regex(pattern.Value, Options);
        Match match = regex.Match(new string(input.Value));
        return match.Success ? new SqlChars(match.Groups[name.Value].Value) : SqlChars.Null;
    }
}

(取自http://msdn.microsoft.com/en-us/magazine/cc163473.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-26
    • 2012-09-08
    • 1970-01-01
    • 2016-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多