Create User-Defined Table Type

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Create User-Defined Table Type相关的知识,希望对你有一定的参考价值。

使用 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的主要内容,如果未能解决你的问题,请参考以下文章

zabbix user-defined item

User-Defined Types in C

MongoDB安全:创建角色(User-Defined Roles)

37.12. User-Defined Aggregates

Hive之UDFs(User-Defined Functions )

翻译:用户变量(User-Defined Variable)(已提交到MariaDB官方手册)