个人观点:
1、表变量缺省放在内存,速度快,所以在触发器,存储过程里如果数据量不大,应该用表变量。
2、临时表缺省使用硬盘,一般来说速度比较慢,那是不是就不用临时表呢?也不是,在数据量比较大的时候,如果使用表变量,会把内存耗尽,然后使用TEMPDB的空间,这样主要还是使用硬盘空间,但同时把内存基本耗尽,增加了内存调入调出的机会,反而降低速度。这种情况建议先给TEMPDB一次分配合适的空间,然后使用临时表。
- declare @tb table(id int identity(1,1) not null,name varchar(20)) --声明表变量
- declare @RowCount int
- set @RowCount=5
- while @RowCount<>0
- begin
- insert @tb select @RowCount
- set @RowCount=@RowCount-1
- end
- select * from @tb
- if object_ID(N'tempdb..#tb') is null --object_ID 对象标识列
- begin
- create table #tb(id int identity(11,1) not null,name varchar(20)) --创建临时表
- insert #tb select name from @tb
- end
- else
- begin
- drop table #tb
- end
- select * from #tb
- drop table #tb