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