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

2021-06-21 12:42:50 字數 2373 閱讀 7171

在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;

在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...