sqlserver 暫存資料表建立 查詢 刪除
create table temptablename
(
id int identity (1,1) not null,
a1 varchar(50),
a2 varchar(50),
a3 varchar(50),
primary key (id) --定義id為暫存資料表#tmp的主鍵
)
select [欄位1,欄位2,...,] into #tmp from table
查詢暫存資料表的資料 select * from #tmp
刪除暫存資料表 drop table #tmp
建立一個不被自動回收的全域暫存資料表。沒有經過測試,雖然貌似看上去沒啥意義,和常規表有什麼區別?說不定以後有用,所以還是記下來:
use master;
go
if object_id('dbo.sp_globals') is not null
drop proc dbo.sp_globals
go
create proc dbo.sp_globals
as
create table ##globals
(
id varchar(36) not null primary key
value varchar(500)
)
go
↑sp內容
exec dbo.sp_procoption 'dbo.sp_globals','startup','true';
只有顯示刪除##globals才會被清除
註明
本地暫存資料表的名稱以單個數字記號 (#) 打頭;它們僅對當前的使用者串連是可見的;當使用者從 sql server 執行個體中斷連線時被刪除。全域暫存資料表的名稱以兩個數字記號 (##) 打頭,建立後對任何使用者都是可見的,當所有引用該表的使用者從 sql server 中斷連線時被刪除。
表變數相當於ado的recordset,速度比暫存資料表快得多。
表變數不能用在下列語句中:
insert into table_variable exec 預存程序。
select select_list into table_variable 語句。
在定義 table 變數的函數、預存程序或批處理結束時,自動清除 table 變數。
但暫存資料表支援。
.表變數速度比暫存資料表快得多(如果記憶體足夠)
如果資料量不大: