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)

时间: 2024-08-26 21:49:21

Create User-Defined Table Type的相关文章

[Hive - LanguageManual] Create/Drop/Alter Database Create/Drop/Truncate Table

Hive Data Definition Language Hive Data Definition Language Overview Create/Drop/Alter Database Create/Drop/Truncate Table Alter Table/Partition/Column Create/Drop/Alter View Create/Drop/Alter Index Create/Drop Function Create/Drop/Grant/Revoke Roles

how to use table type in .net

1. Create Table type in Sqlserver2008. CREATE TYPE dbo.WordTable as table ( [WordText] [nchar](100) NULL, [WordCount] [int] NULL ) And the target table is: CREATE TABLE [dbo].[A_WordCount]( [id] [int] IDENTITY(1,1) NOT NULL, [WordText] [nchar](100) N

[Err] 1214 - The used table type doesn&#39;t support FULLTEXT indexes

-- -- Table structure for table `film_text` -- -- InnoDB added FULLTEXT support in 5.6.10. If you use an -- earlier version, then consider upgrading (recommended) or -- changing InnoDB to MyISAM as the film_text engine -- CREATE TABLE film_text ( fil

[GraphQL] Create an Input Object Type for Complex Mutations

When we have certain mutations that require more complex input parameters, we can leverage the Input Object Type in GraphQL. In this video, we’ll learn how to create an Input Object Type and how to add it to a GraphQL Mutation Type. const express = r

Value too large for defined data type的解决方法之一

折腾了两三天,尝试了网上各种办法最后终于把这个给解决了. 进入正题 之前用的是arm-2009的编译链,在不是共享文件夹下编译的话一切都正常,可以生成想要的目标文件,一旦放入/mnt/hgfs/xxx/(xxx表示自己创建的共享文件夹名称)然后进行make指令后就会出现Value too large for defined data type无奈之下改安装arm-2012的交叉工具链,得以解决.可以保留之前安装的arm-2009不影响后者工作,但要记得进入cd ~vi .bashrc中的末行添加

【翻译自mos文章】在12c中Create or Truncate Table时非常慢,等待事件为 DFS Lock Handle wait

来源于: Create or Truncate Table Slow in 12c While Waiting for DFS Lock Handle wait (文档 ID 2085308.1) APPLIES TO: Oracle Database - Enterprise Edition - Version 12.1.0.2 and later Information in this document applies to any platform. SYMPTOMS In 12c dat

“Unable to create the django_migrations table (%s)

环境:python3.6 + django2.1 +mysql5.5执行python manage.py migrate时报错如下 "Unable to create the django_migrations table (%s)" % exc django.db.migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table ((1064, "You ha

报错 raise MigrationSchemaMissing(&quot;Unable to create the django_migrations table (%s)&quot; % exc)

Django 执行迁移生成表: python manage.py migrate 报错: raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc) 原因: Django2.1不再支持MySQL5.5,必须5.6版本以上 解决办法: 二选一 (1)Django降级到2.0 pip install Django==2.0.0 -i https://pypi.douban.c

[TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers

Using the optional “+” sign together with mapped type modifiers, we can create more explicit and readable type declarations. We can also use the “-” (minus) sign to remove optional declarations from properties. For example, we have an interface: inte