11

假设以下定义:

/// <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).

4

2 回答 2

25

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

/// <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, ...)]可以使用语法添加到返回值。

于 2008-12-10T15:07:25.783 回答
2

另请参阅如何使用 Nvarchar(max) 参数创建 CLR 存储过程,您将了解如何/为什么真正应该使用 SqlChars 数据类型。请参阅在 MSDN中的 CLR 中处理大对象 (LOB) 参数。

于 2015-06-29T17:13:15.810 回答