共用方式為


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

更新:2007 年 11 月

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

注意事項:

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

注意事項:

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

建立使用者定義型別

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

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

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

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

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

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

    注意事項:

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

  6. 若為 Visual Basic 和 Visual C#,請在 [方案總管] 中,開啟 [TestScripts] 資料夾,並按兩下 [Test.sql] 檔案。

    若為 Visual C++,請在 [方案總管] 中,按兩下 [debug.sql] 檔案。

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

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

  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;
    }
}
#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 User-Defined Types, add the following to your debug.sql file:
//
// CREATE TABLE test_table (column1 Point)
//
// 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
//
// DROP TABLE test_table
//
[Serializable]
[SqlUserDefinedType(Format::Native)]
public value struct Point : public INullable
{
private:
    Int32 m_x;
    Int32 m_y;
    bool is_Null;

public:
    property Int32 X
    {
        Int32 get() { return (this->m_x); }
        void set(Int32 value) { m_x = value; }
    }

    property Int32 Y
    {
        Int32 get() { return (this->m_y); }
        void set(Int32 value) { m_y = value; }
    }

    virtual property bool IsNull
    {
        bool get() { return is_Null; }
    }

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

    virtual String ^ToString() override
    {
        if (this->IsNull)
        {
            return "NULL";
        }
        else
        {
            return this->m_x + ":" + this->m_y;
        }
    }


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

        // Parse input string here to separate out coordinates
        String ^str = Convert::ToString(s);
        array<String ^> ^xy = str->Split(':');

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


    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 + SqlString(" center");
        }
        else if (m_y > 0)
        {
            stringReturn = stringReturn + SqlString(" top");
        }
        else if (m_y < 0)
        {
            stringReturn = stringReturn + SqlString(" bottom");
        }

        return stringReturn;
    }
};

將程式碼加入至專案中 TestScripts 資料夾的 Test.sql (在 Visual C++ 中為 debug.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 專案

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 資料庫偵錯