HOW TO:建立及執行 CLR SQL Server 彙總

更新:2007 年 11 月

將 [彙總] 項目加入至 SQL Server 專案,建立 SQL 彙總。成功部署後,以 Managed 程式碼所建立的彙總,其呼叫和執行方式會類似其他 SQL Server 彙總的方式。

注意事項:

Microsoft SQL Server 中的 Common Language Runtime (CLR) 整合功能預設為關閉,在使用 SQL Server 專案項目時則必須啟用這個功能。若要啟用 CLR 整合,請使用 sp_configure 預存程序的 clr enabled 選項。如需詳細資訊,請參閱啟用 CLR 整合

注意事項:

SQL Server 彙總需要實作四個特定方法‎:Init、Accumulate、Merge 和 Terminate。如需詳細資訊,請參閱《SQL 線上叢書》的<SQL CLR .NET 使用者定義彙總函式>主題。

注意事項:

您所看見的對話方塊與功能表命令可能會與 [說明] 所描述的有所不同,視您所使用的設定或版本而定。如果要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定

建立 SQL Server 彙總

若要建立 SQL Server 彙總

  1. 開啟現有的 [SQL Server 專案],或建立一個新專案。如需詳細資訊,請參閱 HOW TO:建立 SQL Server 專案

  2. 從 [專案] 功能表中選取 [加入新項目]。

  3. 加入新項目對話方塊中,選取 [彙總]。

  4. 為新彙總輸入 [名稱]。

  5. 加入執行彙總時執行的程式碼。請參閱底下的第一個範例。

注意事項:

C++ 範例必須使用 /clr:safe 編譯器選項進行編譯。

  1. 將彙總部署至 SQL Server。如需詳細資訊,請參閱 HOW TO:將 SQL Server 專案項目部署至 SQL Server

  2. 在 SQL Server 上執行彙總,進行偵錯。請參閱下列第二個範例。

範例

這個範例會建立可計算母音個數的彙總,此彙總會計算字串資料型別之資料行中的母音個數。此彙總包含下列四個可以多執行緒執行的必要方法:Init、Accumulate、Merge 和 Terminate。

Imports System
Imports System.Data.SqlTypes
Imports 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 Function
End Structure
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();
    }
}
#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 執行它,進行測試,並驗證是否傳回正確資料。這個查詢會針對連絡人資料表的 LastNames 資料行中的所有值傳回母音個數的結果集。

SELECT LastName, COUNT(LastName) AS CountOfLastName, dbo.CountVowels(LastName) AS CountOfVowels
FROM Person.Contact
GROUP BY LastName
ORDER BY LastName

請參閱

工作

HOW TO:建立 SQL Server 專案

HOW TO:建立及執行 CLR SQL Server 預存程序

HOW TO:建立及執行 CLR SQL Server 觸發程序

HOW TO:建立及執行 CLR SQL Server 彙總

HOW TO:建立及執行 CLR SQL Server 使用者定義函式

HOW TO:建立及執行 CLR SQL Server 使用者定義型別

逐步解說:使用 Managed 程式碼建立預存程序

HOW TO:偵錯 SQL CLR 預存程序

概念

SQL Server CLR 整合簡介 (ADO.NET)

使用 Managed 程式碼建立資料庫物件的好處

SQL Server 專案的項目範本

參考

SQL Server 專案和資料庫物件的屬性

其他資源

SQL CLR 資料庫偵錯