Create user-defined Table Type

Source: Internet
Author: User

Creating a custom table type using the Create type, Syntax is similar to the syntax for CREATE table.

CREATETYPE[schema_name.]type_name{ fromBase_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. Phonenumfromvarchar(one notNULL ;

2,creating a user-defined table type

/*Create a user-defined table type*/CREATETYPE dbo. Locationtabletype as TABLE(LocationIDbigint  not NULL Primary Key Clustered, LocationNameVARCHAR( -) not NULL, Costratefloat Check(costratebetween 0.0  and 1.0) )GO

3,drop user-defined Type

drop type dbo. Locationtabletype


Remarks

The DROP TYPE statement would not execute if any of the following is true:

    • there is tables in the database that contain columns of the alias data type O R the user-defined type. Information about alias or user-defined type columns can is obtained by querying the sys.columns or sys.column_type_usages Catalog views.

    • there is 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 is obtained by querying the Sys.sql_expression_dependencies catalog view.

    • there is functions, stored procedures, or triggers created in the Databa SE, and these routines use variables and parameters of the alias or user-defined type. Information about alias or user-defined type parameters can is obtained by querying the sys.parameters or sys.parameter_ty Pe_usages catalog views.

Reference doc:

CREATE TYPE (Transact-SQL)

DROP TYPE (Transact-SQL)

Create user-defined Table Type

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.