博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQLServer temporary table and table variable
阅读量:4363 次
发布时间:2019-06-07

本文共 5333 字,大约阅读时间需要 17 分钟。

Temporary tables are created in tempdb.  The name "temporary" is slightly misleading, for even though the tables are instantiated in tempdb and backed by physical disk and are even logged into the transaction log.  They act like regular tables in that you can query their data via SELECT queries and modify their data via UPDATE, INSERT, and DELETE statements.  If created inside a stored procedure they are destroyed upon completion of the stored procedure.  Furthermore, the scope of any particular temporary table is the session in which it is created; meaning it is only visible to the current user.  Multiple users could create a temp table named #TableX and any queries run simultaneously would not affect one another - they would remain autonomous transactions and the tables would remain autonomous objects.  You may notice that my sample temporary table name started with a "#" sign.  This is the identifier for SQL Server that it is dealing with a temporary table.

There are two types temporary table. One is session scope, another one is global scope.

1. Session scope temporary table

the identifier of a session scope temporary table must be start with '#', below is an example.Sesson scope temporary table can have clustered and nonclutered index.It only visiable to current session, mutiple session will have a differenttamporary table instance. It will auto destoryed at the end of the session. If it defined in a procedure, then it will be destoryed at the end of the procedure.

create table #table1(                               column1 int primary key,                               column2 int,                               column3 int,                               column4 int                              );create noclustered index idx_table1_column2 on #table1(column2);

2. Global scope temporary table

the identifier of a global scope temporary table must be start with '##', below is an example.

Global scope temporary table can have clustered and nonclutered index.It is visiable to all the session,

it will be destroyed when restart SQLServer.

create table ##table2(                               column1 int primary key,                               column2 int,                               column3 int,                               column4 int                              ); create noclustered index idx_table2_column2 on ##table2 (column2);

 

3. Table variable in T-SQL

In a procedue we can have a table variable, below is a example for how to create it. The table variable can have single column primary key or composit primary key of several columns. We can have clustered index on the primary key, but we can have nonclusted index.Below are two example about how to define table variable.

declare @table3  table (                               column1 int primary key,                               column2 int,                               column3 int,                               column4 int                               );
declare @table4  table (                               column1 int,                               column2 int,                               column3 int,                               column4 int                               primary key(column1,column2)                               );

The table variable exactly is also instantiated in the tempdb. Some one thought it was a memory table,but that's not true. 

Below is an experiment to show it is stored in tempdb.

/* Check the difference between Temp Table and Memory Tables */-- Get Current Session IDSELECT @@SPID AS Current_SessionID-- Check the space usage in page filesSELECT user_objects_alloc_page_countFROM sys.dm_db_session_space_usageWHERE session_id = (SELECT @@SPID )GO-- Create Temp Table and insert three thousand rowsCREATE TABLE #TempTable (Col1 INT)INSERT INTO #TempTable (Col1)SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)FROM sys.all_objects aCROSS JOIN sys.all_objects bGO-- Check the space usage in page filesSELECT user_objects_alloc_page_countFROM sys.dm_db_session_space_usageWHERE session_id = (SELECT @@SPID )GO-- Create Table Variable and insert three thousand rowsDECLARE @temp TABLE(Col1 INT)INSERT INTO @temp (Col1)SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)FROM sys.all_objects aCROSS JOIN sys.all_objects bGO-- Check the space usage in page filesSELECT user_objects_alloc_page_countFROM sys.dm_db_session_space_usageWHERE session_id = (SELECT @@SPID )GO-- Clean upDROP TABLE #TempTableGO

 

Temporary table:

  • Table variables can have Clustered and Non-Clustered Indexes
  • You can have constraints 
  • You can create default values on columns
  • Statistics can be created against table variables

Table variable have certain clear limitations: 

  • Table variables can not have Non-Clustered Indexes,but it can have clustered index
  • You can not create constraints in table variables
  • You can not create default values on table variable columns
  • Statistics can not be created against table variables

Common for temporary table and table variable

  • Instantiated in tempdb
  • Clustered indexes can be created on table variables and temporary tables
  • Both are logged in the transaction log
  • Just as with temp and regular tables, users can perform all Data Modification Language (DML) queries against a table variable:  SELECT, INSERT, UPDATE, and DELETE.

转载于:https://www.cnblogs.com/princessd8251/p/3671414.html

你可能感兴趣的文章
【BZOJ 1233】 [Usaco2009Open]干草堆tower (单调队列优化DP)
查看>>
07-3. 数素数 (20)
查看>>
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
查看>>
计算两个日期之间的天数
查看>>
Android关于buildToolVersion与CompileSdkVersion的区别
查看>>
袋鼠云日志,日志分析没那么容易
查看>>
缓存穿透 缓存雪崩 缓存并发
查看>>
了解你的Linux系统:必须掌握的20个命令
查看>>
js setInterval 启用&停止
查看>>
knockoutJS学习笔记04:监控属性
查看>>
Linux下启动/关闭Oracle
查看>>
session和cookie的区别
查看>>
alert弹出窗口,点击确认后关闭页面
查看>>
oracle问题之数据库恢复(三)
查看>>
单点登陆(SSO)
查看>>
HR,也确实“尽职尽责”
查看>>
MaxComputer 使用客户端配置
查看>>
20190823 顺其自然
查看>>
阅读《余生有你,人间值得》有感
查看>>
每日英语
查看>>