在oracle預存程序中建立暫存資料表

來源:互聯網
上載者:User

標籤:

在oracle的預存程序中,不能直接使用DDL語句,比如create、alter、drop、truncate等。

那如果我們想在預存程序中建立一張暫存資料表就只能使用動態sql語句了:

create or replace procedure pro as  str_sql varchar2(100);begin  -- 建立暫存資料表  str_sql := 'create global temporary table temp_table (       col1 varchar2(10),       col2 number    ) on commit preserve rows';  execute immediate str_sql;  -- 使用暫存資料表  str_sql := 'insert into temp_table(col1, col2) values(''a'', 1)';  execute immediate str_sql;  -- 刪除暫存資料表  str_sql := 'drop table temp_table';  execute immediate str_sql;end;

在oracle中,暫存資料表分為會話層級(session)和事務層級(transaction)兩種。

會話級的暫存資料表在整個會話期間都存在,直到會話結束;事務層級的暫存資料表資料在transaction結束後消失,即commit/rollback或結束會話時,

會清除暫存資料表資料。

on commit preserve rows -- 會話層級暫存資料表(退出登入會結束會話)

on commit delete rows -- 事務層級暫存資料表(提交或復原會結束事務)


暫存資料表優缺點:

1. 在僅僅查詢資料時建議使用遊標。

2. 暫存資料表不會建立索引,所以如果資料量比較大或進行多次查詢時,不推薦使用。

在oracle預存程序中建立暫存資料表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.