Use of SQL staging tables, local and global temporary tables

Source: Internet
Author: User

One. Create a local temporary table in SQL

Method 1:

CREATE TABLE #本地临时表名称

Or

CREATE TABLE # #全局临时表名称

Method 2:

SELECT * into #本地临时表名称 the From table name

SELECT * into # #全局临时表名称 from table name

Description

1. The temporary table is actually a user table that is placed in tempdb.

2. The local temporary table begins with "#", and the global temporary table begins with "# #", which exists during the session and is automatically deleted at the end of the session.

Ii. deletion of temporary tables

Grammar:

DROP TABLE #本地临时表名称

Or

DROP TABLE # #全局临时表名称

Description

The drop TABLE statement explicitly drops the temporary table, or the temporary table is automatically dropped by the system when it exits its scope:
1. When the stored procedure is complete, the local temporary table created in the stored procedure is automatically dropped. This table can be referenced by all nested stored procedures that are executed by the stored procedure that created the table. However, the process calling the stored procedure that created this table cannot reference this table;
2. All other local temporary tables are automatically removed at the end of the current session ;
3. The Global temporary table is automatically dropped when the session that created the table ends and other tasks stop referencing them . The association between a task and a table is persisted only within the lifetime of a single Transact-SQL statement. In other words, when the session that created the global temporary table ends, the last Transact-SQL statement referencing this table is finished and the table is automatically dropped.

Use of SQL staging tables, local and global temporary tables

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.