Oracle 預存程序 PROCEDURE

來源:互聯網
上載者:User

標籤:com   not   mit   else   using   資料庫系統   遊標   otf   when   

預存程序  

    一組用於完成特定資料庫功能的SQL語句集,該SQL語句集經過編譯後儲存在資料庫系統中。在使用時候,使用者通過指定已經定義的預存程序名字並給出相應的預存程序參數來調用並執行它,從而完成一個或一系列的資料庫操作。

    包含三部分:過程聲明,執行過程部分,預存程序異常(可選)。

樣本

create or replace procedure sp_(  p_sResult   out integer,--傳回值   p_ID       in   number --ID) as      v_id       number(16);      cursor  v_chac_cr  is                select t.money from T001 t   where t.sid=p_ID;--遊標begin   open v_chac_cr();   loop        fetch v_chac_cr into v_id;         exit when v_chac_cr%notfound;        insert into F002(cnt)  values (v_id);   end loop;   close v_chac_cr;   commit;   p_sResult:=1;exception--例外處理   when others then   p_sResult:=0;   rollback;end sp_;  

 項目運用

  1.擷取表下一個ID值

CREATE OR REPLACE PROCEDURE PNEXTID( tablename   IN   VARCHAR2 , idno     OUT   NUMBER)IS sqlstring   VARCHAR2 ( 500 );BEGIN sqlstring := ‘SELECT nvl(max(id),0)+1 FROM ‘ ||tablename;  execute immediate sqlstring into idno;               -- 動態執行  merge into tSequence a using ( select idno as ID, tablename as Name from dual) b   --組合成表   merge into 確定表  on (upper(a.Name)=upper(b.Name))                       --表名大寫化  when matched then update set a.ID= case when b.ID>a.ID then b.ID else a.ID+ 1 end   -- update;  case when   then  else  end;  when not matched then insert (ID, Name ) values (b.ID,b.Name);                           --insert;  select ID into idno from tSEQUENCE WHERE upper( name ) = upper(tablename);EXCEPTION  WHEN OTHERS THEN   idno := - 1 ;END pNextID;

 2.返回列表資料(遊標)

CREATE OR REPLACE PROCEDURE SP_CONTENT (      o_result out types.cursorType, --結果集      ip_pwf  in varchar2) --關聯流程     as   /*  --型別宣告是遊標變數    create or replace package TestPackage is type outlist is ref cursor; */  v_sql varchar2(1000);  vn_count  numeric(12,0);  vn_num    numeric(12,0);  vn_id     numeric(12,0);  vs_table  varchar2(30);  vn_inst numeric(12,0);begin   vn_count:=length(ip_pwf);   vn_num:=instr(ip_pwf,‘.‘);   vn_id:=to_number(substr(ip_pwf,1,vn_num-1));   vs_table:=substr(ip_pwf,vn_num+1,vn_count-vn_num); begin v_sql:=‘select nvl(max(job_code),0)  from ‘||vs_table||‘ where company = ‘||vn_id||‘‘; execute immediate v_sql into vn_inst; exception   when others then   vn_inst:=0; end; open o_result for select caller,enddate,content from tuser t where class_code < vn_inst;end SP_CONTENT;

  

 

 

Oracle 預存程序 PROCEDURE

聯繫我們

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