資料庫建立臨時表

2021-09-01 15:36:20 字數 576 閱讀 1978

表名前使用乙個#號,臨時表是區域性的,使用兩個#號,臨時表是全域性的,在斷開連線後sql會自動刪除臨時表

create table #a

(id int,

name varchar(50)

)insert into #a(id,name) values(1,'123')

select * from #a

drop table #a

臨時表除了名稱前多了#號外,其他操作與普通表完全一樣。

tb_student是已建立好的表,我們通過臨時表temp把tb_student表中的內容複製到tb_lizi表中,可以使用如下的**實現:

use mcf

select * into #temp from tb_student

select * into tb_lizi from #temp

執行後斷開sql連線並重新連線(也可以退出sq再l重新啟動sql),發現tb_lizi表中的內容tb_student表中的內容完全一致,實現了複製,同時我們沒有用**刪除temp表,但mcf資料庫中卻沒有temp表了,這是因為斷開連線時sql自動刪除了temp表

資料庫中建立臨時表

語法 create table albums artist char 30 album name char 50 media type int go該錶也可以使用下邊的命令來手動刪除 drop table albums go當使用者退出sql server 時該表也可以被自動地刪除如果你是在自態sq...

SQL資料庫臨時表建立和臨時表拼接查詢

當初由於資料庫設計的有些不合理,有平常的查詢很難達到想要的效果,就上網查詢了臨時表查詢,然後進行拼接 begin 判斷臨時表是否存在刪除臨時儲存表 if object id tempdb te is not null begin drop table te end if object id temp...

資料庫臨時表

建立方法 方法一 createtable temptablename 或select 欄位1,欄位2,into temptablename from table 方法二 createtable tempdb mytemptable tidint 說明 1 臨時表其實是放在資料庫tempdb裡的乙個使...