The syntax is as follows:
1. Description: copy a table (only copy structure, source table name: a new table name: B) (access available)
Method 1: Select * into B from a where 1 <> 1
Method 2: Select top 0 * into B from
2. Description: copy a table (copy data, source table name: A target table name: B) (access available)
Insert into B (a, B, c) Select D, E, F from;
3. Create Table # mytemptable (COLA int primary key)
Insert into # mytemptable values (1)
4. Select * into # tmptb from table
Release of temporary tables:
Temporary table
You can create local and global temporary tables. The local temporary table is only visible in the current session; the global temporary table is visible in all sessions.
The name of the local temporary table is preceded by a number character (# table_name), and the name of the global temporary table is preceded by two numbers (# table_name ).
The SQL statement uses the name specified for table_name In the CREATE TABLE statement to reference the temporary table:
Create Table # mytemptable (COLA int primary key)
Insert into # mytemptable values (1)
If a local temporary table is created by a stored procedure or an application executed by multiple users at the same time, SQL Server must be able to differentiate the tables created by different users. Therefore, SQL Server adds a digital suffix to the table name of each local temporary table internally. The full name of a temporary TABLE stored in the sysobjects table of the tempdb database is composed of the TABLE name specified in the create table statement and the digital suffix generated by the system. To allow append suffixes, the table name table_name specified for the local temporary table cannot exceed 116 characters.
Unless the drop table statement is used to explicitly remove a temporary TABLE, the system automatically removes the temporary TABLE when it exits its scope:
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.
All other local temporary tables are automatically removed at the end of the current session.
The global temporary table is automatically removed when the session for this table is created and other tasks are stopped to reference it. 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.