1. 程式人生 > >sql臨時表表變數的使用方法與什麼時候用最好

sql臨時表表變數的使用方法與什麼時候用最好

表變數查詢:

declare @temp table  --表變數
(
	id int ,
	text nvarchar(50), 
	pid int 
) 
insert into @temp select Code as id,FieldContent as text,0 as pid from dbo.S_FieldContent where Field='QMModule'
select id ,text ,pid  from @temp

查詢結果如下:查詢完之後表變數自動消失,表變數存在於記憶體中,速度快。但是資料多的時候不建議使用。

臨時表查詢

create table #temp  --臨時表
(
	id int ,
	text nvarchar(50), 
	pid int 
) 
insert into #temp select Code as id,FieldContent as text,0 as pid from dbo.S_FieldContent where Field='QMModule'
select * from #temp
drop table #temp

查詢結果如下:查詢完之後臨時表還在存在於磁碟中,如果不刪除臨時表可多次使用,速度比表變數慢。但是資料多的時候建議使用。