如何在 SQL Server 中使用 Create 语句创建临时表? [英] How to create temp table using Create statement in SQL Server?

查看:28
本文介绍了如何在 SQL Server 中使用 Create 语句创建临时表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何像创建普通表一样创建临时表?

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

示例:

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

推荐答案

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

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.

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

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

这篇关于如何在 SQL Server 中使用 Create 语句创建临时表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆