sqlserver 临时表的用法

用法: 用于复杂查询时可以用临时表来暂存相关记录,能够提高效率、提高程序的可读性,类似于游标中的
my_cursor declare my_cursor cursor scroll
for select 字段 from tablename
临时表分为:用户临时表和系统临时表。
系统临时表和用户临时表的区别:
1)用户临时表:用户临时表的名称以#开头;
用户临时表的周期只存在于创建这个表的用户的Session,对其他进程是不可见。
当创建它的进程消失时此临时表自动删除
2)系统临时表:系统临时表的名称以##开头
全局临时表对整个sql Server实例都可见,但所有访问它的Session都消失的时候,它也自动删除,如:重启数据库
创建临时表格式:
1)
<div class="codetitle"><a style="CURSOR: pointer" data="18514" class="copybut" id="copybut18514" onclick="doCopy('code18514')"> 代码如下:

<div class="codebody" id="code18514">
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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...