SQLServer temporary table and table variable
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 ID
SELECT @@SPID AS Current_SessionID
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Create Temp Table and insert three thousand rows
CREATE TABLE #TempTable (Col1 INT)
INSERT INTO #TempTable (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Create Table Variable and insert three thousand rows
DECLARE @temp TABLE(Col1 INT)
INSERT INTO @temp (Col1)
SELECT TOP 3000 ROW_NUMBER() OVER(ORDER BY a.name)
FROM sys.all_objects a
CROSS JOIN sys.all_objects b
GO
-- Check the space usage in page files
SELECT user_objects_alloc_page_count
FROM sys.dm_db_session_space_usage
WHERE session_id = (SELECT @@SPID )
GO
-- Clean up
DROP TABLE #TempTable
GO

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.
SQLServer temporary table and table variable的更多相关文章
- sqlserver不能直接create table as select
sqlserver不能直接create table as select 在sqlserver 下想复制一张表的,想到oracle下直接create table xxx as select * from ...
- InnoDB INFORMATION_SCHEMA Temporary Table Info Table
InnoDB INFORMATION_SCHEMA Temporary Table Info Table INNODB_TEMP_TABLE_INFO提供有关InnoDB实例中当前活动的用户创建的In ...
- 表优化 altering table OPTIMIZE TABLE `sta_addr_copy`
表优化 altering table OPTIMIZE TABLE `sta_addr_copy` [总结] 1.实际测试的结果是,在state sqlaltering table OPTIMIZE ...
- mysqldump:Couldn't execute 'show create table `tablename`': Table tablename' doesn't exist (1146)
遇到了一个错误mysqldump: Couldn't execute 'show create table `CONCURRENCY_ERRORS`': Table INVOICE_OLD.CONCU ...
- ORA-01747: user.table.column, table.column 或列说明无效
Oracle.DataAccess.Client.OracleException ORA-01747: user.table.column, table.column 或列说明无效 原因1: 查了一下 ...
- Oracle10g 回收站及彻底删除table : drop table xx purge
drop后的表被放在回收站(user_recyclebin)里,而不是直接删除掉.这样,回收站里的表信息就可以被恢复,或彻底清除. 1.通过查询回收站user_recyclebin获取被删除的表信息, ...
- user.table.column, table.column 或列说明无效
Oracle统计采用别名出错(user.table.column, table.column 或列说明无效) >>>>>>>>>>>& ...
- lua中打印所以类型功能实现table嵌套table
lua中打印所以类型功能实现 本人測试 number.string.bool.nil.table嵌套table.userdata没问题 共享一下有什么问题请拍砖 代码例如以下 cclog = func ...
- 使用vue的v-for生成table , 给table加上序号
现在有一个使用mybatis的分页插件生成的table,table中数据是通过vue获得的 , 前台显示使用<tr v-for="item in items"> 后台v ...
随机推荐
- 4.用文本编辑器输入课堂上练习的Hello.java,并在JDK环境下编译和运行。请将程序编译、运行的结果截图,填入下框中。
一开始报错是因为在文本框了的:用的是中文下的,应该用英文下的;
- 应用服务器上部署自己的 blog 和 wiki 组件。
协作性应用程序 这就是 Web 2.0 的全部,尽管该术语出现才几乎一年的时间,但现在好像只有烹饪杂志还没有加入到讨论 Web 2.0 未来出路的行列中.自从出现了里程碑式的文章 "What ...
- [APAC]自动发送邮件
$lastMon = ((get-date).AddDays(-7) -split(" "))[0] $lastFri = ((get-date).AddDays(-3) -spl ...
- JAVA WEB 的JSP(9*9乘法表+*型金字塔)
运行环境及工具: (Tomcat7) + (JAVA JDK)+ (Eclipse for J2EE) 输出9*9乘法表 代码片段的练习 增加一些简单的JS功能 <%@ page import= ...
- Objective-C、C++和swift 的运行效率比较
自己做iOS开发,以后慢慢都要转swift,前段时间看到网上的一个帖子,说swift的运行效率奇低,觉得自己有必要验证一下. 我用了一个最简单的加法运算,从0加到10000000,看三种语言的时耗. ...
- JavaScript系列:函数 自执行 表达式 声明 定义
可用方式 (function($) {})(jQuery); !function( $ ){}(jQuery); +function( $ ){}(jQuery); -function( $ ){}( ...
- spring boot结合thymeleaf
1.在pom文件中加入thymeleaf相关的依赖 spring-boot-starter-thymeleaf 2.在resource文件夹下创建 template文件夹,在template文件夹中创 ...
- Nginx 笔记与总结(3)配置虚拟主机
Nginx 重启的另外一种方式,相当于 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`: /usr/local/nginx/sbin/nginx -s ...
- PHP+jQuery 注册模块的改进之二:激活链接的URL设置与有效期
接<PHP+jQuery 注册模块的改进之一>继续修改: ①在注册成功后返回登录邮件页面( maillogin.php ),在页面中用户可以点击链接跳转到自己注册邮箱的登录页面,可以再次发 ...
- PHP--进行模块化设计
PHP--进行模块化设计 [来源] 达内 [编辑] 达内 [时间]2012-10-30 导航模块可以简单列为一个关于三级页面链接的HTML文件.通常你可以通过用另一种颜色来标明对当前区域的链 ...