您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

oracle之存储过程,临时表,游标示例

2024/3/19 5:39:26发布17次查看
总结如下: ddl是一种消耗资源非常大的操作,运行时尽量不要使用ddl语句,应用程序需要的临时表应在运行之前就开始创建。不必在每个存储过程中创建一次。临时表总是存在的,他们作为对象存在于数据字典中,并且总是保持为空,直到有会话在其中放入数据 1 创
总结如下:
ddl是一种消耗资源非常大的操作,运行时尽量不要使用ddl语句,应用程序需要的临时表应在运行之前就开始创建。不必在每个存储过程中创建一次。临时表总是存在的,他们作为对象存在于数据字典中,并且总是保持为空,直到有会话在其中放入数据
1 创建临时表
sql代码 
create global temporary table 表名 

  id               varchar2(100 char), 
  name         varchar2(100 char)  

on commit preserve rows; 
2 创建存储过程
sql代码 
create or replace procedure proc_xxx( 
mycur out sys_refcursor 
as 
type my_curtype is ref cursor; 
cur_1 my_curtype; 
tempa varchar2; 
tempb varchar2; 
--此处可声明更多变更^_^ 
begin
open cur_1 for  select * from 表名;
--使用前先清空 
    execute immediate 'truncate table  临时表表名';
loop 
  fetch cur_1 into  tempa; 
  exit when cur_1%notfound;
--进行相关的业务查询,将结果返回于更多变量上,插入临时表数据       
     tempa:='1'; 
     tempb:='jack'; 
     insert into 临时表表名(id,name)values(tempa,tempb);  
     commit;   
end loop;     
  open mycur for  select * from  临时表表名;       
  close  cur_1; 
  message :='查询临时表成功'; 
  exception 
  when others then 
   message :='查询临时表失败'; 
end  proc_xxx; 
参考更多
1 创建临时表,插入数据,返回结果集
sql代码 
create or replace procedure report_month_responsibility( 
 o_cur out sys_refcursor 

is 
str varchar2(200); 
tb_count int; 
begin 
  --先判断全局临时表是否存在,没存在则重新建立: 
  select count(*) into tb_count from dba_tables where table_name='reprottest'; 
  if tb_count=0 then 
    str:=' create global temporary table reprottest( 
           id int, 
           aname varchar2(20) 
    ) on commit preserve rows'; 
     execute immediate str; 
  end if;
str:='insert into reprottest(id,aname)  values(1,''1'')'; 
  execute immediate str; 
  commit; 
  str:='select * from reprottest'; 
  open o_cur for str; -- 给游标变量赋值 
end report_month_responsibility; 
2 调用存储过程
sql代码 
create or replace procedure proc_x() 
is 
  v_id int; 
  v_aname varchar2(20);       
   --定义游标: 
   v_account_cur sys_refcursor; 
begin 
     --调用存储过程: 
       report_month_responsibility(v_account_cur);        
       fetch v_account_cur into v_id,v_aname; 
      --用循环显示游标中的记录: 
       while v_account_cur%found loop 
             dbms_output.put_line('the value of column id is: '||v_id);--打引列id 
             dbms_output.put_line('the value of column aname is: '||v_aname);   
             --打引列aname           
             fetch v_account_cur into v_id,v_aname; 
       end loop; 
       close v_account_cur;     
       execute immediate 'truncate table reprottest';    
end proc_x;
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product