T-SQL tips(1)临时表和表变量

基本原则:能用表变量就用表变量。实在不行才使用临时表。
如与动态语句结合、外部需要使用等,就需要临时表

表变量主要开销系统的内存,而临时表则使用tempdb。对于小数据量的中间数据存储,可以使用表变量,而当需要临时保存的数据量很庞大时,建议使用临时表。具体使用表变量还是临时表,可以根据系统的运行状况来调整。


declare @tb table(id int,name varchar(50),age int--创建表变量

insert @tb select 1,'nn',14
union all select 1,'nn',14

select * from @tb




create table #t(id int,name varchar(50),years int,nums int)--创建临时表

insert #t select 1,'nn',14,15
union all select 1,'nn',14,15
insert into #t  exec sp_gets  --可以用于存储过程或动态SQL结合

select * from #t
drop table #t
posted @ 2006-03-06 21:14  jierry  阅读(2076)  评论(1编辑  收藏  举报