Home > Database > Mysql Tutorial > body text

sqlserver 临时表的用法

WBOY
Release: 2016-06-07 18:01:18
Original
1189 people have browsed it

用于复杂查询时可以用临时表来暂存相关记录,能够提高效率、提高程序的可读性,类似于游标中的my_cursor declare my_cursor cursor scroll

用法:

用于复杂查询时可以用临时表来暂存相关记录,能够提高效率、提高程序的可读性,类似于游标中的
my_cursor declare my_cursor cursor scroll
for select 字段 from tablename
临时表分为:用户临时表和系统临时表。
系统临时表和用户临时表的区别:
1)用户临时表:用户临时表的名称以#开头;
用户临时表的周期只存在于创建这个表的用户的Session,对其他进程是不可见。
当创建它的进程消失时此临时表自动删除。
2)系统临时表:系统临时表的名称以##开头
全局临时表对整个SQL Server实例都可见,但所有访问它的Session都消失的时候,它也自动删除,如:重启数据库。
创建临时表格式:
1)
代码如下:
create table TempTableName
(
ID int IDENTITY (1,1) not null,
a1 varchar(50),
a2 varchar(50),
a3 varchar(50),
primary key (ID) --定义ID为临时表#Tmp的主键
)

2)select [字段1,字段2,...,] into #Tmp from table


查询临时表的数据 select * from #Tmp


删除临时表 drop table #Tmp


清空临时表的所有数据和约束 truncate table #Tmp
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!