SQL temporary table usage Summary

Source: Internet
Author: User
1. Creation method:
Method 1:
Create Table temptablename
Or
Select [Field 1, Field 2,...,] into temptablename from table
Method 2:
Create Table Tempdb . Mytemptable (tid int)
Note:
(1) temporary tables are actually a user table in the database tempdb;
(2) temptablename must contain "#" and "#" can be one or two tables starting with # (local) or # (global, this table exists during the session and is automatically deleted after the session ends;
(3) If you do not start with # Or # when creating the table, but use tempdb. temptable to name it, the table can exist until the database is restarted.
2. manually delete
Drop table temptablename
Note:

The drop TABLE statement explicitly removes the temporary table. Otherwise, the temporary table will be automatically removed 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 removed. All nested stored procedures executed by the stored procedure of the created table can reference this table. However, the process that calls the stored procedure to create 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 removed when the session for this table is created and other tasks are stopped. The association between tasks and tables is only maintained during the lifecycle of a single Transact-SQL statement. In other words, when the session for creating a global temporary table ends, the table is automatically removed after the last Transact-SQL statement that references the table is completed.
3. ExampleCode
(1) Create

Code
Use Testdb

-- Create a local temporary table
Create table # tmpstudent (TID int , name varchar ( 50 ), age int )

Insert Into# TmpstudentValues('Xiaowang',25)

Select * From# Tmpstudent

--Create a local temporary table
Select *Into# TmpstudentFromStudent

Select * From# Tmpstudent

Method 2:

Create   Table Tempdb. mytemptable (TID Int ) -- You have the corresponding permissions to write the data in this way.

(2) Delete Drop Table# Tmpstudent

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.