我可以开发一个SQL/CLR函数吗?它可以接受任何类型的参数,比如MAX()



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

如本文所述(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.RegularExpression命名空间(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)

最新更新