在ORACLE儲存過程中建立臨時表

2021-08-31 17:54:47 字數 2688 閱讀 5765

在oracle儲存過程中建立臨時表

儲存過程裡不能直接使用ddl語句,所以只能使用動態sql語句來執行

--on commit delete rows 說明臨時表是事務指定,每次提交後oracle將截斷表(刪除全部行)

--on commit preserve rows 說明臨時表是會話指定,當中斷會話時oracle將截斷表。

create or replace procedure temptest

(p_searchdate in date)

is v_count int;

str varchar2(300);

begin

v_count := 0;

str:='drop table sett_dailytest';

execute immediate str;

str:='create global temporary table sett_dailytest (

naccountid number not null,

nsubaccountid number not null)

on commit preserve rows';

execute immediate str; ----使用動態sql語句來執行

str:='insert into sett_dailytest (select naccountid,nsubaccountid from sett_dailyaccountbalance)';

execute immediate str;

end temptest;

上面建立乙個臨時表的儲存過程

下面是執行一些操作,向臨時表寫資料。

create or replace procedure pr_dailycheck

( p_date in date,

p_office in integer,

p_currency in integer,

p_check in integer,

p_countnum out integer)

is v_count int;

begin

v_count := 0;

if p_date is null then

dbms_output.put_line('日期不能為空');

else

if p_check = 1 then

insert into sett_dailytest (select naccountid,nsubaccountid from sett_dailyaccountbalance

where dtdate = p_date);

select

count(sd.naccountid) into v_count

from sett_subaccount ss,sett_account sa,sett_dailytest sd

where sd.naccountid = sa.id and sd.nsubaccountid = ss.id and sa.id = ss.naccountid

and sa.nofficeid = p_office and sa.ncurrencyid = p_currency

and rownum < 2;

commit;

p_countnum := v_count;

dbms_output.put_line(p_countnum);

end if;

if p_check = 2 then

insert into sett_dailytest (select naccountid,nsubaccountid from sett_dailyaccountbalance

where dtdate = p_date);

select

count(sd.naccountid) into v_count

from sett_cfsubaccount ss,sett_account sa,sett_dailytest sd

where sd.naccountid = sa.id and sd.nsubaccountid = ss.id and sa.id = ss.naccountid

and sa.nofficeid = p_office and sa.ncurrencyid = p_currency

and rownum < 2;

commit;

p_countnum := v_count;

dbms_output.put_line(p_countnum);

end if;

end if;

end pr_dailycheck;

儲存過程中有一條語句是execute immediate 'create table ta (id integer)';當執行該儲存過程時,可能會報「ora-01031: 許可權不足」的錯誤。

這是因為預設情況下,資料庫對儲存過程在編譯階段進行許可權檢測,資料庫檢測儲存過程的所有者是否擁有直接賦予的許可權,而不是通過乙個角色等間接賦予的許可權。但是在建立儲存過程的時候使用了authid current_user這個選項,那麼語句執行的許可權將在執行過程中根據執行者的許可權進行判斷。

解決的方法有兩個:一是直接授權使用者ddl許可權,二是使用authid current_user選項。

在ORACLE儲存過程中建立臨時表

create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on mit preserve rows execute im...

在ORACLE儲存過程中建立臨時表

create procedure pro asstr varchar2 100 begin str create global temporary table tablename col1 varchar2 10 col2 number on commit preserve rows execute...

在ORACLE儲存過程中建立臨時表

在oracle儲存過程中建立臨時表 儲存過程裡不能直接使用ddl語句,所以只能使用動態sql語句來執行 on commit delete rows 說明臨時表是事務指定,每次提交後oracle將截斷表 刪除全部行 on commit preserve rows 說明臨時表是會話指定,當中斷會話時or...