SQL Server Temp Table create query Delete

Source: Internet
Author: User
Tags number sign

SQL Server Temp Table create query Delete

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
)

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


Query temporary table Data select * FROM #tmp


Delete temporary table drop table #tmp

Create a global temporary table that is not automatically recycled. Not tested, though seemingly meaningless, what's the difference from a regular table? Might be useful later, so still write down:

Use master;
Go
If object_id (' dbo.sp_globals ') is not null
  drop proc Dbo.sp_globals
Go
Create proc Dbo.sp_globals
As
CREATE TABLE # #globals
(
ID varchar (+) NOT NULL primary key
Value varchar (500)
)
Go
↑SP Content

exec dbo.sp_procoption ' dbo.sp_globals ', ' Startup ', ' true ';

Only show Delete # #globals才会被清除


Indicate
The names of local temporary tables start with a single number sign (#), they are visible only to the current user connection, and are deleted when the user disconnects from the SQL Server instance. The name of the global temporary table begins with a two digit sign (# #), which is visible to any user after it is created, and is removed when all users referencing the table are disconnected from SQL Server.


Table variables are equivalent to ADO's Recordset, much faster than temporary tables.
Table variables cannot be used in the following statements:

INSERT INTO table_variable exec stored procedure.
Select select_list into table_variable statement.

The table variable is automatically cleared when the function, stored procedure, or batch that defines the table variable is finished.
But temporary table support.
. Table variables are much faster than temporary tables (if memory is sufficient)
If the amount of data is small:

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.