Oracle資料庫--PL/SQL預存程序和函數的建立和調用

來源:互聯網
上載者:User

標籤:io   ar   strong   資料   on   cti   bs   line   資料庫   

1.預存程序建立的格式:

create or replace procedure My_Procedure is
begin
--執行部分(函數內容);
end;
/

例子:(以hr表為例)

create or replace procedure insert_procedure is
begin
insert into JOBS values(‘MY_JOB‘,‘My Job‘,5000,10000);
end;
/

2.預存程序調用

begin
insert_procedure();
end
/

3.函數的建立格式:

CREATE OR REPLACE FUNCTION function_name(arg1 type1)
RETURN return_type
is
--類型.變數的聲明部分
BEGIN
--執行部分
RETURN expression
EXCEPTION
--異常處理部分
END function_name;

例子:(以hr表為例)

create or replace function update_min_salary return NUMBER
is
n NUMBER;
begin
update JOBS set MIN_SALARY=5000 where MIN_SALARY=4000;
n:=sql%rowcount;
dbms_output.put_line(‘影響的行數:‘||n);
return n;
end;
/

4.調用函數

declare
m NUMBER;
begin
m:=update_min_salary();
DBMS_OUTPUT.PUT_LINE(‘從函數接收到調用此函數影響的函數為:‘||m);
end;
/

5.1

注意:如果要在視窗裡顯示伺服器輸出資訊,要先執行這句:set serveroutput on;

5.2

最後一定要記得用commit提交,否則執行結果不會更新會資料庫。

Oracle資料庫--PL/SQL預存程序和函數的建立和調用

聯繫我們

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