0

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

4

1 回答 1

0

As stated in this post (http://stackoverflow.com/questions/4749071/clr-table-valued-function-with-array-argument?rq=1), table-valued parameters are not supported. SQL/CLR functions take arguments of types in the namespace System.Data.SqlTypes - such as SqlChars, SqlString, SqlInt32, etc. Basically, they are primitive types: no rowsets, no arrays allowed as function parameters (the answer in the previous link proposes work-arounds).

SQL/CLR functions are better suited for things like leveraging the System.Text.RegularExpressions namespace (Regex.Match), or for creating a function that returns a hash of a given value - a password string, perhaps.

Here's an example of SQL functions in a CLR assembly:

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;
    }
}

(taken from http://msdn.microsoft.com/en-us/magazine/cc163473.aspx)

于 2012-11-08T00:55:34.133 回答