如何:創(chuàng)建和運(yùn)行 CLR SQL Server 聚合
通過向 SQL Server 項(xiàng)目添加“聚合”項(xiàng)創(chuàng)建 SQL 聚合。部署成功后,在托管代碼中創(chuàng)建的聚合像其他任何 SQL Server 聚合一樣被調(diào)用和執(zhí)行。
注意; 在默認(rèn)情況下,Microsoft SQL Server 中關(guān)閉了公共語言運(yùn)行庫 (CLR) 集成功能。必須啟用該功能才能使用 SQL Server 項(xiàng)目項(xiàng)。若要啟用 CLR 集成,請使用 sp_configure 存儲過程的“啟用 clr”選項(xiàng)。有關(guān)更多信息,請參見 啟用 CLR 集成。注意; SQL Server 集合要求實(shí)現(xiàn)四個特別方法:Init、Accumulate、Merge 和 Terminate。有關(guān)更多信息,請參見《SQL Books Online》(SQL 聯(lián)機(jī)叢書)中的“SQL CLR .NET User-Defined Aggregate Functions”(SQL CLR .NET 用戶定義的聚合函數(shù))主題。注意; 顯示的對話框和菜單命令可能會與幫助中的描述不同,具體取決于您現(xiàn)用的設(shè)置或版本。若要更改設(shè)置,請在“工具”菜單上選擇“導(dǎo)入和導(dǎo)出設(shè)置”。有關(guān)更多信息,請參見 Visual Studio 設(shè)置。
創(chuàng)建 SQL Server 聚合創(chuàng)建 SQL Server 聚合打開一個現(xiàn)有的“SQL Server 項(xiàng)目”,或者創(chuàng)建一個新項(xiàng)目。有關(guān)更多信息,請參見 如何:創(chuàng)建 SQL Server 項(xiàng)目。
從“項(xiàng)目”菜單中選擇“添加新項(xiàng)”。
在 “添加新項(xiàng)”對話框 中選擇“聚合”。
輸入新聚合的“名稱”。
添加執(zhí)行聚合時要運(yùn)行的代碼。請參見下面的第一個示例。
注意; C++ 示例在編譯時必須使用 /clr:safe 編譯器選項(xiàng)。
將聚合部署到 SQL Server。有關(guān)更多信息,請參見 如何:將 SQL Server 項(xiàng)目項(xiàng)部署到 SQL Server 中。
通過在 SQL Server 上執(zhí)行聚合對其進(jìn)行調(diào)試。請參見下面的第二個示例。
示例此示例創(chuàng)建對元音計數(shù)的聚合。此聚合對字符串?dāng)?shù)據(jù)類型的列中的元音計數(shù)。聚合包含以下四個必需的可運(yùn)行多個線程的方法:Init、Accumulate、Merge 和 Terminate。
Visual Basic 復(fù)制代碼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# 復(fù)制代碼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++ 復(fù)制代碼#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 上運(yùn)行它以進(jìn)行調(diào)試并驗(yàn)證是否返回正確的數(shù)據(jù)。此查詢返回對 Contact 表中 LastNames 列的所有值的元音計數(shù)的結(jié)果集。
復(fù)制代碼SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowelsFROM Person.ContactGROUP BY LastNameORDER BY LastName
