Differences between table variables and custom types of temporary tables

Source: Internet
Author: User
When table variables use custom types, the type is valid in the current database.
When a temporary table uses a custom type, the type is valid in the tempdb database.

The following are three examples:

-- Test Example 1: create a custom type ssnuse tempdbgoif object_id ('tempdb .. # testtable ') is not null drop table # testtable; if exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; gouse testgoif exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; go -- create a custom type create type DBO in test. SSN from Char (9) Not null goraiserror ('table variable defined successfully',) with Nowait; declare @ testtable table (rowid int iden Tity, SSN) goraiserror ('failed to create a temporary table ', 2715) with Nowait; Create Table # testtable (rowid int identity, SSN)/* success message for defining table variables, level 16, Status 7, 3rd rows, 2nd columns, parameters, or variables: the data type SSN cannot be found. */Go

-- Test Example 2: create a custom ssnuse testgoif exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; gouse tempdbgoif object_id ('tempdb .. # testtable ') is not null drop table # testtable; if exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; go -- create a custom type create type DBO in tempdb. SSN from Char (9) Not NULL; gouse testgoraiserror ('table definition variable failed',) with Nowait; declare @ testtable ( Rowid int identity, SSN) goraiserror ('temporary table created successfully', 2715) with Nowait; Create Table # testtable (rowid int identity, SSN)/* message, level 16, Status 7, 3rd rows, 2nd columns, parameters, or variables: the data type SSN cannot be found. Temporary table created */go

-- Test example 3: create a custom ssnuse tempdbgoif object_id ('tempdb .. # testtable ') is not null drop table # testtable; if exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; gocreate type DBO. SSN from Char (9) Not NULL; gouse testgoif exists (select 1 from sys. types where name = 'ssn') Drop type DBO. SSN; gocreate type DBO. SSN from Char (9) Not NULL; goraiserror ('table variable defined successfully',) with Nowait; declare @ testtable table (rowid int identity, SSN) goraiserror ('temporary table created successfully',) with Nowait; Create Table # testtable (rowid int identity, SSN)/* Table variable created successfully temporary table created successfully */

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.