假设以下定义:

/// <summary>
/// Replaces each occurrence of sPattern in sInput with sReplace. This is done
/// with the CLR:
/// new RegEx(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace).
/// The result of the replacement is the return value.
/// </summary>
[SqlFunction(IsDeterministic = true)]
public static  SqlString FRegexReplace(string sInput, string sPattern,
      string sReplace)
{
    return new Regex(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace);
}

传递长度大于4000的nvarchar(max)sInput值将导致该值被截断(即,调用此UDF的结果是nvarchar(4000)而不是nvarchar(max)

最佳答案

哦,无论如何,我自己找到了答案:

/// <summary>
/// Replaces each occurrence of sPattern in sInput with sReplace. This is done
/// with the CLR:
/// new RegEx(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace).
/// The result of the replacement is the return value.
/// </summary>
[SqlFunction(IsDeterministic = true)]
[return: SqlFacet(MaxSize = -1)]
public static  SqlString FRegexReplace([SqlFacet(MaxSize = -1)]string sInput,
       string sPattern, string sReplace)
{
    return new Regex(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace);
}

这个想法是要向SQL Server提示输入和返回值不是默认的nvarchar(4000),但是具有不同的大小。

我学到了关于属性的新技巧:可以将它们添加到参数以及方法本身(很明显),也可以添加到[return: AttributeName(Parameter=Value, ...)]语法的返回值中。

10-06 06:36