Oracle函数的定义
生活随笔
收集整理的這篇文章主要介紹了
Oracle函数的定义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、函數
? 函數是作為數據庫對象存儲在oracle數據庫中,函數又被稱為PL/SQL子程序。oracle處理使用系統提供的函數之外,用戶還可以自己定義函數。函數通常被作為一個表達式來調用或存儲過程的一個參數,具有返回值。通常用于返回特定的數據。 函數語法如下:
create or replace function 函數名稱 (
參數名稱 測試類型,
參數名稱 測試類型
)
return 數據類型
is
自定義變量名 數據類型
begin
處理語句;
return 自定義變量名;
exception
異常處理語句;
end;
函數和存儲過程類似,只是函數必須有返回值。
二、實例
1、沒有參數的函數
create or replace function test return varchar2 is beginreturn 'hello world'; end test; -- 函數調用 begindbms_output.put_line(test()); end2、有輸入參數的函數
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; -- 函數調用 begin dbms_output.put_line(get_name(1)); end;3、有帶輸入和輸出的函數
create or replace function get_perons_info(f_id number,f_age out number ) return varchar2 isv_name varchar2(50); --必須有聲明長度 beginselect username, age into v_name, f_age from person where id = f_id;return v_name; end get_perons_info; -- 函數調用 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、帶有輸入輸出參數的函數
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; -- 函數調用 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、函數返回游標
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('獲取信息發生錯誤'); end get_person_all; --函數調用 declarec_cursor sys_refcursor;r_person person%rowtype; beginc_cursor := get_person_all();--2、打開游標 -- open c_cursor; --此處不需要顯示地打開游標,因為調用存儲過程的時候返回的游標已經打開了--3、提取數據loopfetch c_cursor into r_person;exit when c_cursor%notfound; -- 下面沒有數據的時候,退出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;?三、函數其他命令
重新編譯函數
alter function 函數名稱 compile;刪除函數
drop function 函數名稱;查看指定的函數
select * from dba_objects where object_name = '函數名稱(字母大寫)' and object_type ='FUNCTION';?
轉載于:https://www.cnblogs.com/ChenNotepad/p/4044768.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Oracle函数的定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LA 3890 (半平面交) Most
- 下一篇: UG如何切换模块