sqlserver 暫存資料表建立 查詢 刪除

來源:互聯網
上載者:User

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 變數。
但暫存資料表支援。
.表變數速度比暫存資料表快得多(如果記憶體足夠)
如果資料量不大:

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.