如何:創建和運行 CLR SQL Server 聚合
通過向 SQL Server 項目添加“聚合”項創建 SQL 聚合。部署成功后,在托管代碼中創建的聚合像其他任何 SQL Server 聚合一樣被調用和執行。
注意; 在默認情況下,Microsoft SQL Server 中關閉了公共語言運行庫 (CLR) 集成功能。必須啟用該功能才能使用 SQL Server 項目項。若要啟用 CLR 集成,請使用 sp_configure 存儲過程的“啟用 clr”選項。有關更多信息,請參見 啟用 CLR 集成。注意; SQL Server 集合要求實現四個特別方法:Init、Accumulate、Merge 和 Terminate。有關更多信息,請參見《SQL Books Online》(SQL 聯機叢書)中的“SQL CLR .NET User-Defined Aggregate Functions”(SQL CLR .NET 用戶定義的聚合函數)主題。注意; 顯示的對話框和菜單命令可能會與幫助中的描述不同,具體取決于您現用的設置或版本。若要更改設置,請在“工具”菜單上選擇“導入和導出設置”。有關更多信息,請參見 Visual Studio 設置。
創建 SQL Server 聚合創建 SQL Server 聚合打開一個現有的“SQL Server 項目”,或者創建一個新項目。有關更多信息,請參見 如何:創建 SQL Server 項目。
從“項目”菜單中選擇“添加新項”。
在 “添加新項”對話框 中選擇“聚合”。
輸入新聚合的“名稱”。
添加執行聚合時要運行的代碼。請參見下面的第一個示例。
注意; C++ 示例在編譯時必須使用 /clr:safe 編譯器選項。
將聚合部署到 SQL Server。有關更多信息,請參見 如何:將 SQL Server 項目項部署到 SQL Server 中。
通過在 SQL Server 上執行聚合對其進行調試。請參見下面的第二個示例。
示例此示例創建對元音計數的聚合。此聚合對字符串數據類型的列中的元音計數。聚合包含以下四個必需的可運行多個線程的方法:Init、Accumulate、Merge 和 Terminate。
Visual Basic 復制代碼Imports SystemImports System.Data.SqlTypesImports Microsoft.SqlServer.Server
<Serializable()> _<SqlUserDefinedAggregate(Format.Native)> _Public Structure CountVowels
' count only the vowels in the passed-in strings Private countOfVowels As SqlInt32
Public Sub Init() countOfVowels = 0 End Sub
Public Sub Accumulate(ByVal value As SqlString) Dim stringChar As String Dim indexChar As Int32
' for each character in the given parameter For indexChar = 0 To Len(value.ToString()) - 1
stringChar = value.ToString().Substring(indexChar, 1)
If stringChar.ToLower() Like '[aeiou]' Then
' it is a vowel, increment the count countOfVowels = countOfVowels + 1 End If Next End Sub
Public Sub Merge(ByVal value As CountVowels)
Accumulate(value.Terminate()) End Sub
Public Function Terminate() As SqlString
Return countOfVowels.ToString() End FunctionEnd StructureC# 復制代碼using System;using System.Data.SqlTypes;using Microsoft.SqlServer.Server;
[Serializable][SqlUserDefinedAggregate(Format.Native)]public struct CountVowels{ // count only the vowels in the passed-in strings private SqlInt32 countOfVowels;
public void Init() { countOfVowels = 0; }
public void Accumulate(SqlString value) { // list of vowels to look for string vowels = 'aeiou'; // for each character in the given parameter for (int i=0; i < value.ToString().Length; i++) { // for each character in the vowels string for (int j=0; j < vowels.Length; j++) { // convert parameter character to lowercase and compare to vowel if (value.Value.Substring(i,1).ToLower() == vowels.Substring(j,1)) { // it is a vowel, increment the count countOfVowels+=1; } } } }
public void Merge(CountVowels value) { Accumulate(value.Terminate()); }
public SqlString Terminate() { return countOfVowels.ToString(); }}C++ 復制代碼#include 'stdafx.h'
#using <System.dll>#using <System.Data.dll>#using <System.Xml.dll>
using namespace System;using namespace System::Data;using namespace System::Data::Sql;using namespace System::Data::SqlTypes;using namespace Microsoft::SqlServer::Server;
// In order to debug your Aggregate, add the following to your debug.sql file://// SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowels// FROM Person.Contact// GROUP BY LastName// ORDER BY LastName//
[Serializable][SqlUserDefinedAggregate(Format::Native)]public value struct CountVowels{public: void Init() { countOfVowels = 0; }
void Accumulate(SqlString value) { // list of vowels to look for String ^vowels = 'aeiou';
// for each character in the given parameter for (int i=0; i < value.ToString()->Length; i++) { // for each character in the vowels string for (int j=0; j < vowels->Length; j++) { // convert parameter character to lowercase and compare to vowel if (value.Value->Substring(i, 1)->ToLower() == vowels->Substring(j, 1)) { // it is a vowel, increment the count countOfVowels+=1; break; } } } }
void Merge(CountVowels value) { Accumulate(value.Terminate()); }
SqlTypes::SqlString Terminate() { return countOfVowels.ToString(); }
private: // count only the vowels in the passed-in strings SqlInt32 countOfVowels;};
部署聚合后,在 SQL Server 上運行它以進行調試并驗證是否返回正確的數據。此查詢返回對 Contact 表中 LastNames 列的所有值的元音計數的結果集。
復制代碼SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowelsFROM Person.ContactGROUP BY LastNameORDER BY LastName
