您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)Oracle中如何使用臨時(shí)表,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
Oracle臨時(shí)表介紹
Oracle的臨時(shí)表temporary tables,一般我們分為兩種臨時(shí)表,分別的會話級的臨時(shí)表和事務(wù)級的臨時(shí)表。
1.會話級臨時(shí)表
會話級臨時(shí)表是指臨時(shí)表中的數(shù)據(jù)只在會話生命周期之中存在,當(dāng)用戶退出會話結(jié)束的時(shí)候,Oracle自動(dòng)清除臨時(shí)表中數(shù)據(jù)。
格式:
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Preserve Rows;
2.事務(wù)級臨時(shí)表
事務(wù)級臨時(shí)表是指臨時(shí)表中的數(shù)據(jù)只在事務(wù)生命周期中存在。
Create Global Temporary Table Table_Name
(
Col1 Type1,
Col2 Type2
...
)
On Commit Delete Rows;
當(dāng)一個(gè)事務(wù)結(jié)束(commit or rollback),Oracle自動(dòng)清除臨時(shí)表中數(shù)據(jù)。
注意
一般我們在SQL里面用到臨時(shí)表都會是用到時(shí)候創(chuàng)建,用完后刪除(或是自動(dòng)刪除),如下:
但是在Oracle里面因?yàn)橛袝捈壓褪聞?wù)級臨時(shí)表,用完后會自動(dòng)清空等,不建議每次都Drop然后再Create,主要原因也是我在測試的過程中發(fā)現(xiàn)用到Drop臨時(shí)表時(shí)報(bào)過錯(cuò)了,當(dāng)時(shí)的錯(cuò)誤沒記下來,后來就是變?yōu)闆]有就創(chuàng)建,有就清空數(shù)據(jù)的寫法。
臨時(shí)表用法
我們直接上代碼
declare vi_count integer;
vs_sSql varchar2(4000):='';
begin
vs_sSql:= 'select count(*) from user_tables where table_name = upper(' ||
chr(39) || 'temp_cstable' || chr(39) || ')';
execute immediate vs_sSql into vi_count;
dbms_output.put_line(vi_count);
--判斷temp_cstable的臨時(shí)表是否存在,如果存在清空里面數(shù)據(jù),不存在即創(chuàng)建
if vi_count>0 then
vs_sSql := 'delete from temp_cstable';
execute immediate vs_sSql;
else
vs_sSql := '
create global temporary table temp_cstable (
incode varchar2(20),
barcode varchar2(20),
xstotal number,
yhtotal number) on commit delete rows';
execute immediate vs_sSql;
end if;
end;
上述就是小編為大家分享的Oracle中如何使用臨時(shí)表了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。