Oracle預存程序及函數

來源:互聯網
上載者:User

標籤:io   ar   sp   strong   on   cti   bs   amp   line   

1.在Oracle中,預存程序包括三部分組成:定義部分、執行部分、和異常處理部分(即例外)

eg1:輸入員工編號,查詢員工的姓名和薪資

create or repalce  procedure mypro2 is

declare --定義部分,定義變數和常量等,變數定義一般以V_開頭,常量定義一般以C_開頭

v_ename varchar2(20);

v_sal number(7,2);

begin--執行部分

select ename,sal into v_ename,v_sal from emp where empno=&no;

dbms_output.put_line(‘員工的姓名是:‘||v_ename||‘工資是‘||v_sal);

exception--異常處理部分

when no_data_found then

dbms_output.put_line(‘您輸入的員工編號不存在‘);

end;

eg2:帶參數的預存程序

--輸入員工的姓名和新的薪資,根據姓名改薪資

create or replace procedure mypro2(pename varchar2,psal number) is

begin

update emp set sal=psal where ename=pename;

end;

預存程序的調用方法:

a:exec mypro1()或者call mypro1();

b:exec mypro1(‘SCOTT‘,1200)或者call mypro1(‘SCOTT‘,1200);

2.函數

函數必須有傳回值的

結構如下:根據僱員名稱求年薪

create or repalce function myfun1(fename varchar2) return number is yearsal nunber;

begin

select sal*12+nvl(comm,0) into yearsal from emp where ename=fename;

return yearsal;

end;

函數的調用方法:

SQL> var ys number;
SQL> call myf1(‘SCOTT‘) into:ys;

 

Oracle預存程序及函數

聯繫我們

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