【发布时间】:2012-08-20 12:51:45
【问题描述】:
例如,我可以自己使用 C# 开发 MAX() 函数吗?谢谢!
【问题讨论】:
-
你能详细说明一下吗?我不清楚你在问什么。
标签: sql-server sqlclr
例如,我可以自己使用 C# 开发 MAX() 函数吗?谢谢!
【问题讨论】:
标签: sql-server sqlclr
如本文所述 (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;
}
}
【讨论】: