SQL Server数据库-临时表

临时表

临时表用法和真实表一模一样
临时表生命周期,只存在当前会回(释放:关闭会话窗口,也可以drop)


数据库临时表 两种:

1.全局临时表

2.普通临时表


 

普通临时表创建

create table #test2(
id int identity(1,1),
name varchar(50) not null
)

select * from #test2--临时表(内存中)

insert into #test2
values('李四')

全局临时表创建

--创建
create table ##test_2(
id int identity(1,1),
name varchar(50) not null
)

--查询
select * from ##test_2   --临时表(内存中)

--插入
insert into ##test_2
values('李四')

--清空
delete ##test_2

--删除
drop table ##test_2

 

 我们不难发现 普通临时表和全局临时表在创建的时候 ,

一个是单个#;

一个是双##;


 

笔记

--临时表:内存有关 DataTable临时表 程序结束 
--临时表用法和真实表一模一样
--临时表生命周期,只存在当前会回(释放:关闭会话窗口,也可以drop)
--数据库临时表 两种:全局临时表、普通临时表
--#

create table test2(
id int identity(1,1),
name varchar(50) not null
)

select * from test2

insert into test2
values('张三')

--第一种:手动创建临时表
create table #test2(
id int identity(1,1),
name varchar(50) not null
)

select * from #test2--临时表(内存中)

insert into #test2
values('李四')

delete #test2

drop table #test2


--第二种:克隆创建临时表
select *
into #students3 from v_GetStudent

select *from #students3

select * from #students2

--克隆数据
insert into #ClassInfos2 --目标
select  classid,classname into ##classinfos3 from ClassInfos --数据源

select *from ##ClassInfos2
drop table #ClassInfos2

 


select *from ##classinfos3
--原因是因为 :普通临时表
--全局临时表,还是内存中
--全局的临时表,当创建它的会话窗口关闭时,消失。
select *from ##classinfos3
--sqlconnection

 
select * from ClassInfos
.fill()--执行网
.close()

 

posted @ 2019-03-06 10:53  橙-极纪元JJYCheng  阅读(539)  评论(0编辑  收藏  举报