Temporary Tables and Table Variable

本文介绍了如何使用SQL Server中的临时表和表变量来处理数据,包括创建、填充数据及返回结果的方法。临时表和表变量各有优缺点,可根据数据量大小选择合适的方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

Temporary Tables

By Bill Graziano

 


 

Sophie writes "Can you use a Stored Procedure to open a table and copy data to a sort of virtual table (or a records set) so that you can change the values with and not affect the actual data in the actual table. And then return the results of the virtual table? Thanks!" This article covers temporary tables and tables variables and is updated for SQL Server 2005.

I love questions like this. This question is just a perfect lead in to discuss temporary tables. Here I am struggling to find a topic to write about and I get this wonderful question. Thank you very much Sophie.

Temporary Tables

The simple answer is yes you can. Let look at a simple CREATE TABLE statement:

CREATE TABLE #Yaks (
YakID int,
YakName char(30) )

You'll notice I prefixed the table with a pound sign (#). This tells SQL Server that this table is a local temporary table. This table is only visible to this session of SQL Server. When I close this session, the table will be automatically dropped. You can treat this table just like any other table with a few exceptions. The only real major one is that you can't have foreign key constraints on a temporary table. The others are covered in Books Online.

Temporary tables are created in tempdb. If you run this query:

CREATE TABLE #Yaks (
YakID int,
YakName char(30) )

select name
from tempdb..sysobjects 
where name like '#yak%'

drop table #yaks

You'll get something like this:

name
------------------------------------------------------------------------------------ 
#Yaks_________________________ . . . ___________________________________00000000001D

(1 row(s) affected)

except that I took about fifty underscores out to make it readable. SQL Server stores the object with a some type of unique number appended on the end of the name. It does all this for you automatically. You just have to refer to #Yaks in your code.

If two different users both create a #Yaks table each will have their own copy of it. The exact same code will run properly on both connections. Any temporary table created inside a stored procedure is automatically dropped when the stored procedure finishes executing. If stored procedure A creates a temporary table and calls stored procedure B, then B will be able to use the temporary table that A created. It's generally considered good coding practice to explicitly drop every temporary table you create.  If you are running scripts through SQL Server Management Studio or Query Analyzer the temporary tables are kept until you explicitly drop them or until you close the session.

Now let's get back to your question. The best way to use a temporary table is to create it and then fill it with data. This goes something like this:

CREATE TABLE #TibetanYaks(
YakID int,
YakName char(30) )

INSERT INTO #TibetanYaks (YakID, YakName)
SELECT 	YakID, YakName
FROM 	dbo.Yaks
WHERE 	YakType = 'Tibetan'

-- Do some stuff with the table

drop table #TibetanYaks

Obviously, this DBA knows their yaks as they're selecting the famed Tibetan yaks, the Cadillac of yaks. Temporary tables are usually pretty quick. Since you are creating and deleting them on the fly, they are usually only cached in memory.

Table Variables

If you are using SQL Server 2000 or higher, you can take advantage of the new TABLE variable type. These are similar to temporary tables except with more flexibility and they always stay in memory.  The code above using a table variable might look like this:

DECLARE @TibetanYaks TABLE (
YakID int,
YakName char(30) )

INSERT INTO @TibetanYaks (YakID, YakName)
SELECT 	YakID, YakName
FROM 	dbo.Yaks
WHERE 	YakType = 'Tibetan'

-- Do some stuff with the table 

Table variables don't need to be dropped when you are done with them.

Which to Use

  • If you have less than 100 rows generally use a table variable.  Otherwise use  a temporary table.  This is because SQL Server won't create statistics on table variables.
  • If you need to create indexes on it then you must use a temporary table.
  • When using temporary tables always create them and create any indexes and then use them.  This will help reduce recompilations.  The impact of this is reduced starting in SQL Server 2005 but it's still a good idea.

Answering the Question

And all this brings us back to your question.  The final answer to your question might look something like this:

DECLARE @TibetanYaks TABLE (
YakID int,
YakName char(30) )

INSERT INTO @TibetanYaks (YakID, YakName)
SELECT 	YakID, YakName
FROM 	dbo.Yaks
WHERE 	YakType = 'Tibetan'

UPDATE 	@TibetanYaks
SET 	YakName = UPPER(YakName)

SELECT *
FROM @TibetanYaks

Global Temporary Tables

You can also create global temporary tables. These are named with two pound signs. For example, ##YakHerders is a global temporary table. Global temporary tables are visible to all SQL Server connections. When you create one of these, all the users can see it.  These are rarely used in SQL Server.

Summary

That shows you an example of creating a temporary table, modifying it, and returning the values to the calling program. I hope this gives you what you were looking for.

### Hive 中 `CREATE TEMPORARY TABLE` 的语法及用法 在 Hive 中,`CREATE TEMPORARY TABLE` 是一种用于创建临时表的命令。这些临时表仅存在于当前会话期间,在会话结束时会被自动删除[^1]。以下是关于该语法及其用法的具体说明: #### 1. 基本语法 ```sql CREATE TEMPORARY TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment], ...)] [COMMENT table_comment] [PARTITIONED BY (col_name data_type [COMMENT col_comment], ...)] [CLUSTERED BY (col_name, col_name, ...) [SORTED BY (col_name [ASC|DESC], ...)] INTO num_buckets BUCKETS] [ROW FORMAT row_format] [STORED AS file_format] [TBLPROPERTIES (property_name=property_value, ...)]; ``` - **`TEMPORARY`**: 表明这是一个临时表,只在当前会话中可见并可用。 - **`IF NOT EXISTS`**: 可选参数,防止重复定义相同的临时表名称。 - **列定义**: 定义表中的列名和数据类型。 - **分区 (`PARTITIONED BY`)**: 如果需要按某些字段进行分区,则可以在此处指定。 - **分桶 (`CLUSTERED BY`)**: 如果需要对数据进行分桶处理,则可在此处设置。 #### 2. 存储位置与性能优化 通过配置属性 `hive.exec.temporary.table.storage`,可以选择将临时表的数据存储在内存、SSD 或默认文件系统中[^1]。这有助于提高查询效率,尤其是在频繁访问小型中间结果集的情况下。 #### 3. 创建方式 除了直接使用 `CREATE TEMPORARY TABLE` 外,还可以利用其他方法快速构建临时表: - 使用 `CREATE TEMPORARY TABLE ... AS SELECT` (CTAS): 这种方式允许基于现有数据源动态生成结构化表格内容。 ```sql CREATE TEMPORARY TABLE temp_table_example AS SELECT column_a, SUM(column_b) FROM source_table GROUP BY column_a; ``` - 结合子查询或复杂逻辑:可以在单次 SQL 查询内部嵌套多个阶段计算过程,并最终输出至目标临时表内[^5]。 #### 4. 注意事项 需要注意的是,在 SparkSQL 环境下并不完全兼容此功能——即不支持显式的 `CREATE TEMPORARY TABLE` 操作;取而代之推荐采用视图形式(`CREATE TEMPORARY VIEW`)作为替代方案[^4]。 --- ### 示例代码 下面提供了一个完整的例子展示如何正确运用上述知识点完成实际开发任务: 假设我们需要从两个不同时间范围内的用户活动记录汇总VIP会员列表: ```sql -- 设置临时表存储介质为内存 SET hive.exec.temporary.table.storage=MEMORY; -- 构造第一个时间段内的活跃UID集合 CREATE TEMPORARY TABLE vip_tmp_early AS SELECT dt, uid FROM user_activity WHERE dt BETWEEN '2023-01-01' AND '2023-01-15'; -- 同理获取第二个区间的结果集 CREATE TEMPORARY TABLE vip_tmp_late AS SELECT dt, uid FROM user_activity WHERE dt BETWEEN '2023-01-16' AND '2023-01-31'; -- 综合分析两部分交叠情况下的共同成员身份特征分布状况 SELECT COUNT(DISTINCT t1.uid), AVG(t2.some_metric) FROM vip_tmp_early t1 LEFT JOIN vip_tmp_late t2 ON t1.uid = t2.uid; ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值