2008-12-10 20 views
11

Giả định nghĩa sau đây:Làm thế nào để trả về một nvarchar (max) trong một UDF CLR?

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

Đi qua trong một giá trị nvarchar(max) cho sInput với chiều dài> 4000 sẽ dẫn đến việc giá trị được cắt ngắn (tức là kết quả của việc gọi UDF này là nvarchar(4000) như trái ngược với nvarchar(max)

Trả lời

24
.

Ồ, bất cứ điều gì, tôi tự tìm thấy câu trả lời:

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

Ý tưởng là gợi ý cho SQL Server rằng đầu vào và giá trị trả lại không phải là mặc định nvarchar(4000), nhưng có kích thước khác.

Tôi đã học được một mẹo mới về các thuộc tính: Chúng có thể được thêm vào các tham số cũng như chính phương thức đó (khá rõ ràng), nhưng cũng là với giá trị trả về theo cú pháp [return: AttributeName(Parameter=Value, ...)].

+0

bạn là một ngôi sao, thưa bạn :) –

+0

Cơ chế đó được mô tả rõ ràng trong tham chiếu tôi đã trích dẫn - câu trả lời bạn đã bỏ phiếu. – bielawski

Các vấn đề liên quan