当前位置: 代码迷 >> SQL >> Sql Server临时表的作用域
  详细解决方案

Sql Server临时表的作用域

热度:46   发布时间:2016-05-05 12:55:19.0
Sql Server临时表的作用域:

Sql Server临时表的作用域:

       临时表与永久表相似,只是它的创建是在Tempdb中,它只有在一个数据库连接结束后或者由SQL命令DROP掉,才会消失,否则就会一直存在。临时表在创建的时候都会产生SQL Server的系统日志,虽它们在Tempdb中体现,是分配在内存中的,它们也支持物理的磁盘,但用户在指定的磁盘里看不到文件。 
  临时表分为本地和全局两种,本地临时表的名称都是以“#”为前缀,只有在本地当前的用户连接中才是可见的,当用户从实例断开连接时被删除。全局临时表的名称都是以“##”为前缀,创建后对任何用户都是可见的,当所有引用该表的用户断开连接时被删除。 


新建临时表(#MyStudents,包含2个字段分别为sName、sAge),并将Mystudents中的相应数据copy其中。

 create table #MyStudents(sName nvarchar(50),sAge int)

 create table ##MyStudents(sName nvarchar(50),sAge int)
 
 insert into #MyStudents
 select FName,fAge from MyStudents
 
 select * from #MyStudents
 
 delete from #MyStudents
 where sName='康凯' or sName='李昂' or sName='李琛'
 
 delete from #MyStudents
 where sName in('康凯','李昂','李琛')
  相关解决方案