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