Oracle函数的定义
一、函數(shù)
? 函數(shù)是作為數(shù)據(jù)庫對(duì)象存儲(chǔ)在oracle數(shù)據(jù)庫中,函數(shù)又被稱為PL/SQL子程序。oracle處理使用系統(tǒng)提供的函數(shù)之外,用戶還可以自己定義函數(shù)。函數(shù)通常被作為一個(gè)表達(dá)式來調(diào)用或存儲(chǔ)過程的一個(gè)參數(shù),具有返回值。通常用于返回特定的數(shù)據(jù)。 函數(shù)語法如下:
create or replace function 函數(shù)名稱 (
參數(shù)名稱 測(cè)試類型,
參數(shù)名稱 測(cè)試類型
)
return 數(shù)據(jù)類型
is
自定義變量名 數(shù)據(jù)類型
begin
處理語句;
return 自定義變量名;
exception
異常處理語句;
end;
函數(shù)和存儲(chǔ)過程類似,只是函數(shù)必須有返回值。
二、實(shí)例
1、沒有參數(shù)的函數(shù)
create or replace function test return varchar2 is beginreturn 'hello world'; end test; -- 函數(shù)調(diào)用 begindbms_output.put_line(test()); end2、有輸入?yún)?shù)的函數(shù)
create or replace function get_name(v_id number ) return varchar2 is --is類似于declarev_name varchar2(50); beginselect username into v_name from person where id = v_id;return v_name; end get_name; -- 函數(shù)調(diào)用 begin dbms_output.put_line(get_name(1)); end;3、有帶輸入和輸出的函數(shù)
create or replace function get_perons_info(f_id number,f_age out number ) return varchar2 isv_name varchar2(50); --必須有聲明長(zhǎng)度 beginselect username, age into v_name, f_age from person where id = f_id;return v_name; end get_perons_info; -- 函數(shù)調(diào)用 declarev_age number;v_name varchar2(255); beginv_name := get_perons_info(1, v_age );dbms_output.put_line('name:'||v_name||' age:'||v_age); end;?4、帶有輸入輸出參數(shù)的函數(shù)
create or replace function get_person_info2(f_id in out number ) return varchar2 isv_name varchar2(50); beginselect username, age into v_name, f_id from person where id = f_id;return v_name; end get_person_info2; -- 函數(shù)調(diào)用 declarev_id number;v_name varchar2(50); beginv_id := 1;v_name := get_person_info2(v_id);dbms_output.put_line('name:'||v_name||' age:'||v_id ); end;?5、函數(shù)返回游標(biāo)
create or replace function get_person_allreturn sys_refcursor isp_cursor sys_refcursor; beginopen p_cursor forselect * from person; return p_cursor;exceptionwhen others thenDBMS_OUTPUT.PUT_LINE('獲取信息發(fā)生錯(cuò)誤'); end get_person_all; --函數(shù)調(diào)用 declarec_cursor sys_refcursor;r_person person%rowtype; beginc_cursor := get_person_all();--2、打開游標(biāo) -- open c_cursor; --此處不需要顯示地打開游標(biāo),因?yàn)檎{(diào)用存儲(chǔ)過程的時(shí)候返回的游標(biāo)已經(jīng)打開了--3、提取數(shù)據(jù)loopfetch c_cursor into r_person;exit when c_cursor%notfound; -- 下面沒有數(shù)據(jù)的時(shí)候,退出dbms_output.put_line('id:'||r_person.id);dbms_output.put_line('username:'||r_person.username);dbms_output.put_line('age:'||r_person.age); end loop; end;?三、函數(shù)其他命令
重新編譯函數(shù)
alter function 函數(shù)名稱 compile;刪除函數(shù)
drop function 函數(shù)名稱;查看指定的函數(shù)
select * from dba_objects where object_name = '函數(shù)名稱(字母大寫)' and object_type ='FUNCTION';?
轉(zhuǎn)載于:https://www.cnblogs.com/ChenNotepad/p/4044768.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的Oracle函数的定义的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LA 3890 (半平面交) Most
- 下一篇: UG如何切换模块