如何在SQL Server中使用Create语句创建临时表?

时间:2022-09-02 15:13:59

How to create a temp table similarly to creating a normal table?

如何创建临时表与创建普通表类似?

Example:

例:

CREATE TABLE table_name 
(
    column1 datatype,
    column2 datatype,
    column3 datatype,
     ....
 );

2 个解决方案

#1


11  

A temporary table can have 3 kinds, the # is the most used. This is a temp table that only exists in the current session. An equivalent of this is @, a declared table variable. This has a little less "functions" (like indexes etc) and is also only used for the current session. The ## is one that is the same as the #, however, the scope is wider, so you can use it within the same session, within other stored procedures.

临时表可以有3种,#是最常用的。这是仅存在于当前会话中的临时表。相当于@的是一个声明的表变量。这有一点“功能”(如索引等),也只用于当前会话。 ##是与#相同的,但是,范围更广,因此您可以在同一会话中,在其他存储过程中使用它。

You can create a temp table in various ways:

您可以通过各种方式创建临时表:

declare @table table (id int)
create table #table (id int)
create table ##table (id int)
select * into #table from xyz

#2


19  

Same thing, Just start the table name with # or ##:

同样的事情,只需用#或##启动表名:

CREATE TABLE #TemporaryTable          -- Local temporary table - starts with single #
(
    Col1 int,
    Col2 varchar(10)
    ....
);

CREATE TABLE ##GlobalTemporaryTable   -- Global temporary table - note it starts with ##.
(
    Col1 int,
    Col2 varchar(10)
    ....
);

Temporary table names start with # or ## - The first is a local temporary table and the last is a global temporary table.

临时表名以#或##开头 - 第一个是本地临时表,最后一个是全局临时表。

Here is one of many articles describing the differences between them.

这是描述它们之间差异的许多文章之一。

#1


11  

A temporary table can have 3 kinds, the # is the most used. This is a temp table that only exists in the current session. An equivalent of this is @, a declared table variable. This has a little less "functions" (like indexes etc) and is also only used for the current session. The ## is one that is the same as the #, however, the scope is wider, so you can use it within the same session, within other stored procedures.

临时表可以有3种,#是最常用的。这是仅存在于当前会话中的临时表。相当于@的是一个声明的表变量。这有一点“功能”(如索引等),也只用于当前会话。 ##是与#相同的,但是,范围更广,因此您可以在同一会话中,在其他存储过程中使用它。

You can create a temp table in various ways:

您可以通过各种方式创建临时表:

declare @table table (id int)
create table #table (id int)
create table ##table (id int)
select * into #table from xyz

#2


19  

Same thing, Just start the table name with # or ##:

同样的事情,只需用#或##启动表名:

CREATE TABLE #TemporaryTable          -- Local temporary table - starts with single #
(
    Col1 int,
    Col2 varchar(10)
    ....
);

CREATE TABLE ##GlobalTemporaryTable   -- Global temporary table - note it starts with ##.
(
    Col1 int,
    Col2 varchar(10)
    ....
);

Temporary table names start with # or ## - The first is a local temporary table and the last is a global temporary table.

临时表名以#或##开头 - 第一个是本地临时表,最后一个是全局临时表。

Here is one of many articles describing the differences between them.

这是描述它们之间差异的许多文章之一。