Use of SQL Server temp table _mssql

Source: Internet
Author: User
Usage:

You can use temporary tables to hold related records for complex queries, which can improve efficiency and readability of your programs, similar to those in cursors
My_cursor DECLARE my_cursor cursor scroll
For select field from tablename
Temporary tables are divided into: User temporary tables and system temporary tables.
The difference between a system temp table and a user temporary table:
1 User Temporary table: the name of the user temporary table begins with #;
The cycle of a user's temporary table exists only in the session of the user who created the table, and is not visible to other processes.
This temporary table is automatically deleted when the process that created it disappears.
2 System Temp Table: The name of the system temporary table with # #开头
The Global temporary table is visible to the entire instance of SQL Server, but it is also automatically deleted when all access to its session disappears, such as restarting the database.
To create a temporary table style:
1)
Copy Code code as follows:

CREATE TABLE Temptablename
(
ID int IDENTITY (1,1) NOT NULL,
A1 varchar (50),
A2 varchar (50),
A3 varchar (50),
Primary key (ID)--defines a primary key with an ID of temporary table #tmp
)

2 Select [Field 1, Field 2,...,] into #Tmp from table


Query temporary table Data select * FROM #Tmp


Delete temporary table drop table #Tmp


Clears all data and constraints for a temporary table truncate TABLE #Tmp
Related Article

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.