更新:2007 年 11 月
通过将“用户定义的类型”添加到 SQL Server 项目,可以创建 SQL 用户定义的类型。部署成功后,可以将该类型用在可使用系统类型的所有上下文中。这包括列定义、变量、参数、函数结果、游标、触发器和复制。用户定义的类型为用户提供了 SQL Server 数据类型系统的扩展功能以及定义复杂结构类型的能力。
| .gif) 说明: | 
|---|
| 默认情况下,公共语言运行时 (CLR) 集成功能在 Microsoft SQL Server 中处于关闭状态,必须启用该功能才能使用 SQL Server 项目项。若要启用 CLR 集成,请使用 sp_configure 存储过程的 clr enabled 选项。有关更多信息,请参见启用 CLR 集成。 | 
| .gif) 说明: | 
|---|
| 对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您使用的 Visual Studio 版本及设置。有关更多信息,请参见Visual Studio 设置。 | 
创建用户定义的类型
创建 SQL 用户定义的类型
- 打开一个现有的“SQL Server 项目”,或者创建一个新项目。有关更多信息,请参见如何:创建 SQL Server 项目。 
- 从“项目”菜单中选择“添加新项”。 
- 在“添加新项”对话框 中选择“用户定义的类型”。 
- 键入用户定义的新类型的“名称”。 
- 添加用于定义和创建用户定义的类型的代码。请参见此过程后面的第一个示例。 .gif) 说明: 说明:- C++ 示例在编译时必须使用 /clr:safe 编译器选项。 
- 对于 Visual Basic 和 Visual C#,在“解决方案资源管理器”中,打开“TestScripts”文件夹,再双击“Test.sql”文件。 - 对于 Visual C++,在“解决方案资源管理器”中,双击“debug.sql”文件。 
- 将代码添加到“Test.sql”(Visual C++ 中为“debug.sql”)文件中以执行用户定义的类型。请参见此过程后面的第二个示例。 
- 按 F5 生成、部署并调试该用户定义的类型。有关不进行调试直接部署的信息,请参见如何:将 SQL Server 项目项部署到 SQL Server 中。 
- 查看“输出”窗口中显示的结果,然后选择“显示以下输出: 数据库输出”。 
示例
此示例创建一个 Point 类型,该类型的用法与其他简单类型一样。使用 Serializable 和 SqlUserDefinedTypeAttribute 属性来修饰这个类声明。SqlUserDefinedTypeAttribute 的 Format 属性决定了用户定义的类型的存储格式。该类型通过实现 Parse 和 Parse 方法来实现字符串转换。此外,该类型还实现两个属性过程,以便为此类所表示的点获取和设置 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;
    }
};
将用于执行和测试用户定义类型 (Point) 的代码添加到“Test.sql”(Visual C++ 中为“debug.sql”)文件中,该文件在项目的“TestScripts”文件夹中。例如,若要检查新类型,可以创建一个使用此类型的表。下面的示例演示如何在创建表的过程中使用 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
请参见
任务
如何:创建和运行 CLR SQL Server 用户定义的函数
如何:创建和运行 CLR SQL Server 用户定义的类型