如何在 CLR UDF 中返回 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)]
public static SqlString FRegexReplace(string sInput, string sPattern,
string sReplace)
{
return new Regex(sPattern, RegexOptions.Multiline).Replace(sInput, sReplace);
}
为 sInput
传递一个长度 > nvarchar(max) 值 4000 将导致值被截断(即调用此 UDF 的结果是 nvarchar(4000)
,而不是 nvarchar(max)
。
Assuming following definition:
/// <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);
}
Passing in a nvarchar(max)
value for sInput
with a length > 4000 will result in the value being truncated (i.e. the result of calling this UDF is nvarchar(4000)
as opposed to nvarchar(max)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
哦,无论如何,我自己找到了答案:
这个想法是暗示 SQL Server 输入和返回值不是默认的
nvarchar(4000)
,而是具有不同的大小。我学到了一个关于属性的新技巧:它们可以添加到参数以及方法本身(非常明显),但也使用
[return: AttributeName(Parameter) 添加到返回值=值, ...)]
语法。Oh, whatever, I found the answer myself:
The idea is to hint to SQL Server that the input and return values are not the default
nvarchar(4000)
, but have a different size.I learned a new trick regarding attributes: They can be added to the parameters as well as the method itself (quite obvious), but also to the return value with the
[return: AttributeName(Parameter=Value, ...)]
Syntax.另请参阅如何使用 Nvarchar(max) 参数创建 CLR 存储过程 您将在其中发现真正应该如何/为什么使用 SqlChars 数据类型。 请参阅在 MSDN 中处理 CLR 中的大对象 (LOB) 参数。
See also How to create CLR stored procedure with Nvarchar(max) parameter where you'll discover how/why you really should use the SqlChars data type. See Handling Large Object (LOB) Parameters in the CLR in MSDN.