码迷,mamicode.com
首页 > 其他好文 > 详细

Create User-Defined Table Type

时间:2016-06-15 20:48:26      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

使用 Create type 创建自定义的Table Type ,Syntax 如下,和Create Table的 语法十分相似。

CREATE TYPE [ schema_name. ] type_name
{ 
    FROM base_type 
    [ ( precision [ , scale ] ) ]
    [ NULL | NOT NULL ] 
  | AS TABLE ( { <column_definition> | <computed_column_definition> }
        [ <table_constraint> ] [ ,...n ] )  
} [ ; ]

<column_definition> ::= 
column_name <data_type>
    [ COLLATE collation_name ] 
    [ NULL | NOT NULL ]
    [ 
        DEFAULT constant_expression ] 
      | [ IDENTITY [ ( seed ,increment ) ] 
    ]
    [ ROWGUIDCOL ] [ <column_constraint> [ ...n ] ] 

<column_constraint> ::= 
{     { PRIMARY KEY | UNIQUE } 
        [ CLUSTERED | NONCLUSTERED ] 
        [ WITH ( <index_option> [ ,...n ] ) ]
  | CHECK ( logical_expression ) 
} 

<table_constraint> ::=
{ 
    { PRIMARY KEY | UNIQUE } 
        [ CLUSTERED | NONCLUSTERED ] 
         ( column_name [ ASC | DESC ] [ ,...n ] ) 
        [ WITH ( <index_option> [ ,...n ] )]
    | CHECK ( logical_expression ) 
} 


Examples

1,Creating an alias type based on the varchar data type

CREATE TYPE dbo.PhoneNum
FROM varchar(11) NOT NULL ;

2,Creating a user-defined table type

/* Create a user-defined table type */
CREATE TYPE dbo.LocationTableType 
AS TABLE 
( 
LocationID bigint not null primary key clustered,
LocationName VARCHAR(50) not null,
CostRate float check(CostRate between 0.0 and 1.0) 
)
GO

3,drop user-defined type

drop type dbo.LocationTableType


Remarks  

The DROP TYPE statement will not execute when any of the following is true: 

  • There are tables in the database that contain columns of the alias data type or the user-defined type. Information about alias or user-defined type columns can be obtained by querying the sys.columns or sys.column_type_usages catalog views.

  • There are computed columns, CHECK constraints, schema-bound views, and schema-bound functions whose definitions reference the alias or user-defined type. Information about these references can be obtained by querying the sys.sql_expression_dependencies catalog view.

  • There are functions, stored procedures, or triggers created in the database, and these routines use variables and parameters of the alias or user-defined type. Information about alias or user-defined type parameters can be obtained by querying the sys.parameters or sys.parameter_type_usages catalog views.

参考doc:

CREATE TYPE (Transact-SQL)

DROP TYPE (Transact-SQL)

Create User-Defined Table Type

标签:

原文地址:http://www.cnblogs.com/ljhdo/p/5587095.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!