Share via


HOW TO:使用 Common Language Run-time 整合建立和執行 SQL Server 使用者定義型別

將 [使用者定義型別] 加入至 SQL Server Common Language Runtime (SQL CLR) 資料庫專案,建立 SQL 使用者定義型別。 部署成功後,您就可以在想要使用系統型別的所有內容中使用。 包括資料行定義、變數、參數、函式結果、游標、觸發程式以及複寫。 使用者定義的型別提供使用者 SQL Server 資料型別系統的擴充性,以及定義複雜結構型別的能力。

注意事項注意事項

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。 您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。 如需詳細資訊,請參閱 Visual Studio 設定

建立使用者定義型別

若要建立 SQL 使用者定義型別

  1. 開啟現有的 [SQL CLR 資料庫專案],或建立一個新專案。 如需詳細資訊,請參閱HOW TO:針對使用 SQL Server Common Language Run-time 整合的資料庫物件建立專案

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

  3. 在 [加入新項目] 對話方塊中,選取 [使用者定義型別]。

  4. 為新的使用者定義型別輸入 [名稱]。

  5. 加入程式碼以定義和建立使用者定義的型別。 請參閱遵循此程序的第一個範例。

  6. 開啟 [方案總管] 中的 [TestScripts] 資料夾,並按兩下 Test.sql 檔案。

    注意事項注意事項

    您可以指定其他指令碼做為預設偵錯指令碼。 如需詳細資訊,請參閱HOW TO:編輯 Test.sql 指令碼以執行使用 SQL Server Common Language Run-time 整合的物件

  7. 將程式碼加入至 Test.sql (在 Visual C++ 中為 debug.sql) 檔案,以執行使用者定義的型別。 請參閱遵循此程序的第二個範例。

  8. 按下 F5,即可建置、部署和偵錯使用者定義的型別。 如需如何在不偵錯的情況下進行部署的詳細資訊,請參閱 HOW TO:將 SQL CLR 資料庫專案項目部署至 SQL Server

    重要事項重要事項

    SQL Server 2005 和 SQL Server 2008 僅支援使用 .NET Framework 2.0、3.0 或 3.5 版所建置的 SQL Server 專案。 如果您嘗試部署SQL Server專案,SQL Server 2005或SQL Server 2008,將顯示錯誤消息:Deploy error (SQL01268): .NET SqlClient Data Provider: Msg 6218, Level 16, State 3, Line 1 CREATE ASSEMBLY for assembly 'AssemblyName' failed because assembly 'AssemblyName' failed verification. Check if the referenced assemblies are up-to-date and trusted (for external_access or unsafe) to execute in the database(在進行校驗是您要部署的程式集的名稱)。 如需詳細資訊,請參閱HOW TO:針對使用 SQL Server Common Language Run-time 整合的資料庫物件建立專案

  9. 查看所示的結果輸出視窗和選擇顯示輸出:資料庫輸出

範例

這個範例建立型別 Point,其使用的方法與其他的簡單型別相同。 使用 Serializable 和 SqlUserDefinedTypeAttribute 屬性 (Attribute) 裝飾類別宣告。 SqlUserDefinedTypeAttribute 的 Format 屬性決定使用者定義型別的儲存格式。 此型別藉由實作 Parse 和 ToString 方法,以實作字串轉換。 此型別也實作兩個屬性 (Property) 程序,為這個類別所代表的點取得並設定 X 和 Y 值。

Imports System
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server

<Serializable()>
<SqlUserDefinedType(Format.Native)>
Public Structure Point
    Implements INullable

    Private m_x As Int32
    Private m_y As Int32
    Private is_Null As Boolean


    Public Property X() As Int32
        Get
            Return (Me.m_x)
        End Get
        Set(ByVal Value As Int32)
            m_x = Value
        End Set
    End Property


    Public Property Y() As Int32
        Get
            Return (Me.m_y)
        End Get
        Set(ByVal Value As Int32)
            m_y = Value
        End Set
    End Property


    Public ReadOnly Property IsNull() As Boolean Implements INullable.IsNull
        Get
            Return is_Null
        End Get
    End Property


    Public Shared ReadOnly Property Null() As Point
        Get
            Dim pt As Point = New Point
            pt.is_Null = True
            Return pt
        End Get
    End Property


    Public Overrides Function ToString() As String
        If Me.IsNull() Then
            Return Nothing
        Else
            Return Me.m_x & ":" & Me.m_y
        End If
    End Function


    Public Shared Function Parse(ByVal s As SqlString) As Point
        If s = SqlString.Null Then
            Return Null
        End If

        If s.ToString() = SqlString.Null.ToString() Then
            Return Null
        End If

        If s.IsNull Then
            Return Null
        End If

        'Parse input string here to separate out coordinates
        Dim str As String = Convert.ToString(s)
        Dim xy() As String = str.Split(":"c)

        Dim pt As New Point()
        pt.X = CType(xy(0), Int32)
        pt.Y = CType(xy(1), Int32)
        Return (pt)
    End Function


    Public Function Quadrant() As SqlString

        If m_x = 0 And m_y = 0 Then
            Return "centered"
        End If

        Dim stringResult As String = ""

        Select Case m_x
            Case 0
                stringResult = "center"
            Case Is > 0
                stringResult = "right"
            Case Is < 0
                stringResult = "left"
        End Select

        Select Case m_y
            Case 0
                stringResult = stringResult & " center"
            Case Is > 0
                stringResult = stringResult & " top"
            Case Is < 0
                stringResult = stringResult & " bottom"
        End Select

        Return stringResult
    End Function
End Structure
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Serializable()]
[SqlUserDefinedType(Format.Native)]
public struct Point : INullable
{
    private Int32 m_x;
    private Int32 m_y;
    private bool is_Null;


    public Int32 X
    {
        get
        {
            return (this.m_x);
        }
        set
        {
            m_x = value;
        }
    }


    public Int32 Y
    {
        get
        {
            return (this.m_y);
        }
        set
        {
            m_y = value;
        }
    }


    public bool IsNull
    {
        get
        {
            return is_Null;
        }
    }


    public static Point Null
    {
        get
        {
            Point pt = new Point();
            pt.is_Null = true;
            return (pt);
        }
    }


    public override string ToString()
    {
        if (this.IsNull)
        {
            return "NULL";
        }
        else
        {
            return this.m_x + ":" + this.m_y;
        }
    }


    public static Point Parse(SqlString s)
    {
        if (s.IsNull)
        {
            return Null;
        }

        // Parse input string here to separate out coordinates
        string str = Convert.ToString(s);
        string[] xy = str.Split(':');

        Point pt = new Point();
        pt.X = Convert.ToInt32(xy[0]);
        pt.Y = Convert.ToInt32(xy[1]);
        return (pt);
    }


    public SqlString Quadrant()
    {
        if (m_x == 0 && m_y == 0)
        {
            return "centered";
        } 

        SqlString stringReturn = "";

        if (m_x == 0)
        {
            stringReturn = "center";
        }
        else if (m_x > 0)
        {
            stringReturn = "right";
        } 
        else if (m_x < 0)
        {
            stringReturn = "left";
        }

        if (m_y == 0) 
        {
            stringReturn = stringReturn + " center";
        }
        else if (m_y > 0)
        {
            stringReturn = stringReturn + " top";
        }
        else if (m_y < 0)
        {
            stringReturn = stringReturn + " bottom";
        }

        return stringReturn;
    }
}

將程式碼加入至專案中 TestScripts 資料夾的 Test.sql 檔案,以執行和測試使用者定義型別 (Point)。 例如,若要檢查新型別,請建立使用這個型別的資料表。 下列範例將示範如何在建立資料表時使用 Point 型別。

CREATE TABLE test_table (column1 Point)
go

INSERT INTO test_table (column1) VALUES ('1:2')
INSERT INTO test_table (column1) VALUES ('-2:3')
INSERT INTO test_table (column1) VALUES ('-3:-4')

select column1.Quadrant() from test_table

請參閱

工作

HOW TO:針對使用 SQL Server Common Language Run-time 整合的資料庫物件建立專案

HOW TO:使用 Common Language Run-time 整合建立和執行 SQL Server 預存程序

HOW TO:使用 Common Language Run-time 整合建立和執行 SQL Server 觸發程序

HOW TO:使用 Common Language Run-time 整合建立和執行 SQL Server 彙總

HOW TO:使用 Common Language Run-time 整合建立和執行 SQL Server 使用者定義函式

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

HOW TO:偵錯 SQL CLR 預存程序

參考

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

概念

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

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

開始以小組開發參考 SQLCLR 物件的資料庫

以 Managed 程式碼建立 SQL Server 物件

其他資源

SQL CLR Database Debugging