`
q272156430
  • 浏览: 270056 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

oracle存储过程语法

阅读更多
  1. 存储过程 包含三部分: 声明,执行部分,异常。    
  2. 可以有无参数程序和带参数存储过程。    
  3. 无参程序语法    
  4. 1 create or replace procedure NoParPro   
  5. 2 as   ;   
  6. 3 begin   
  7. 4  ;   
  8. 5 exception   
  9. 6      ;   
  10. 7 end;   
  11. 8    
  12.   
  13.    带参存储过程实例    
  14.  1 create or replace procedure queryempname(sfindno emp.empno%type) as   
  15.  2        sName emp.ename%type;   
  16.  3        sjob emp.job%type;   
  17.  4 begin   
  18.  5        ....   
  19.  7 exception   
  20.           ....   
  21. 14 end;   
  22. 15    
  23.   
  24.    带参数存储过程含赋值方式    
  25.  1 create or replace procedure runbyparmeters  (isal in emp.sal%type,    
  26.                             sname out varchar,sjob in out varchar)   
  27.  2  as icount number;   
  28.  3  begin   
  29.  4       select count(*) into icount from emp where sal>isal and job=sjob;   
  30.  5       if icount=1 then   
  31.  6         ....   
  32.  9       else  
  33. 10         ....   
  34. 12       end if;   
  35. 13  exception   
  36. 14       when too_many_rows then   
  37. 15       DBMS_OUTPUT.PUT_LINE('返回值多于1行');   
  38. 16       when others then   
  39. 17       DBMS_OUTPUT.PUT_LINE('在RUNBYPARMETERS过程中出错!');   
  40. 18  end;   
  41. 19    
  42.   
  43.   过程调用   
  44.   方式一   
  45.  1 declare   
  46.  2        realsal emp.sal%type;   
  47.  3        realname varchar(40);   
  48.  4        realjob varchar(40);   
  49.  5  begin   
  50.  6        realsal:=1100;   
  51.  7        realname:='';   
  52.  8        realjob:='CLERK';   
  53.  9        runbyparmeters(realsal,realname,realjob);     --必须按顺序   
  54. 10        DBMS_OUTPUT.PUT_LINE(REALNAME||'   '||REALJOB);   
  55. 11  END;   
  56. 12    
  57.   
  58.   方式二   
  59.  1 declare   
  60.  2       realsal emp.sal%type;   
  61.  3       realname varchar(40);   
  62.  4       realjob varchar(40);   
  63.  5 begin   
  64.  6       realsal:=1100;   
  65.  7       realname:='';   
  66.  8       realjob:='CLERK';   
  67.  9       runbyparmeters(sname=>realname,isal=>realsal,sjob=>realjob);  --指定值对应变量顺序可变   
  68. 10       DBMS_OUTPUT.PUT_LINE(REALNAME||'   '||REALJOB);   
  69. 11 END;   
  70. 12   
create or replace procedure prc_update_tb01(Prm_Appcode  OUT VARCHAR2,  --执行代码
                                            Prm_Errormsg OUT VARCHAR2   --错误消息
                                            ) is
    v_root_id  varchar2(10):='510401';--一级网格值(攀枝花市)
    type td02_table_type is TABLE OF td02%ROWTYPE INDEX BY BINARY_INTEGER; 
    td02_TABLE td02_table_type; --td02 网格信息 东区
    
    td02_TABLE1 td02_table_type;  -- 街道网格
    
    td02_TABLE2 td02_table_type;  -- 社区网格
    
    -- 可变数组
    type four_id_type is table of varchar2(100) index by binary_integer; 
    v_four_id four_id_type;--四级网格(社区...)
     
    type three_id_type is table of four_id_type index by binary_integer;
    v_three_id three_id_type;--三级网格(街道办...)
   
    type two_id_type is table of three_id_type index by binary_integer;
    v_two_id two_id_type;--二级网格(东区...)
    
    type one_id_type is table of two_id_type index by binary_integer;
    v_one_id one_id_type;--一级网格(攀枝花市)
    
    v_sql varchar2(100); --sql;
  BEGIN
  
    update tb01 t set ytd021=v_root_id;--更新为攀枝花市
    commit;
   SELECT B.* BULK COLLECT
            INTO td02_TABLE--东区
            FROM td02 B where b.ytd024 =v_root_id;
 
 for a in 1.. td02_TABLE.count loop --更新东区,西区..
       --var_array(a):=td02_TABLE(a).YTD021; 
       update tb01 t set ytd021=td02_TABLE(a).YTD021 where t.aab004 like '%'||td02_TABLE(a).ytd022||'%';
       v_sql:='update tb01 t set ytd021='||CHR(39)
             || td02_TABLE(a).YTD021 || CHR(39)
             ||' where t.aab004 like '||CHR(39)
             ||'%'||td02_TABLE(a).ytd022||'%'||CHR(39); 
       dbms_output.put_line('更新SQL:'||v_sql); 
  end loop; commit;
  
 for j in 1.. td02_TABLE.count loop --更新街道
   SELECT B.* BULK COLLECT
            INTO td02_TABLE1
            FROM td02 B where  b.YTD024 = td02_TABLE(j).ytd021;
    for c in 1.. td02_TABLE1.count loop
       --var_array(a):=td02_TABLE(a).YTD021; 
       update tb01 t set ytd021=td02_TABLE1(c).YTD021 
         where t.aab004 like '%'||td02_TABLE1(c).ytd022||'%';     
    end loop;
 end loop; commit;
 
 for j in 1.. td02_TABLE1.count loop --更新社区
   SELECT B.* BULK COLLECT
            INTO td02_TABLE2
             FROM td02 B 
            where b.YTD024 = td02_TABLE1(j).ytd021;
    for c in 1.. td02_TABLE2.count loop
       --var_array(a):=td02_TABLE(a).YTD021; 
       update tb01 t set ytd021=td02_TABLE2(c).YTD021 where t.aab004 like '%'||td02_TABLE2(c).ytd022||'%';     
    end loop;
 end loop;commit;
  EXCEPTION
    WHEN OTHERS THEN
    Prm_Appcode  := 'fail';
    Prm_Errormsg := 'update tb01 fail!';
    dbms_output.put_line('更新tb01出错..'); 
    RETURN;
end prc_update_tb01;

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics