Oracle之自訂函數

來源:互聯網
上載者:User

標籤:

資料庫中函數包含四個部分:聲明、傳回值、函數體和異常處理。 

 

  1. --沒有參數的函數  
  2. create or replace function get_user return varchar2 is  
  3.   v_user varchar2(50);  
  4. begin  
  5.   select username into v_user from user_users;  
  6.   return v_user;  
  7. end get_user;  
  8.   
  9. --測試  
  10. 方法一  
  11. select get_user from dual;  
  12.   
  13. 方法二  
  14. SQL> var v_name varchar2(50)  
  15. SQL> exec :v_name:=get_user;  
  16.   
  17. PL/SQL 過程已成功完成。  
  18.   
  19. SQL> print v_name  
  20.   
  21. V_NAME  
  22. ------------------------------  
  23. TEST  
  24.   
  25. 方法三  
  26. SQL> exec dbms_output.put_line(‘當前資料庫使用者是:‘||get_user);  
  27. 當前資料庫使用者是:TEST  
  28.   
  29. PL/SQL 過程已成功完成。  

 

  • --帶有IN參數的函數  
  • create or replace function get_empname(v_id in number) return varchar2 as  
  •   v_name varchar2(50);  
  • begin  
  •   select name into v_name from employee where id = v_id;  
  •    return v_name;  
  • exception  
  •   when no_data_found then  
  •     raise_application_error(-20001, ‘你輸入的ID無效!‘);  
  • end get_empname;  

 

附:

函數調用限制
1、SQL語句中只能調用儲存函數(伺服器端),而不能調用用戶端的函數
2、SQL只能調用帶有輸入參數,不能帶有輸出,輸入輸出函數
3、SQL不能使用PL/SQL的特有資料類型(boolean,table,record等)
4、SQL語句中調用的函數不能包含INSERT,UPDATE和DELETE語句

 

查看函數院原始碼
oracle會將函數名及其原始碼資訊存放到資料字典中user_source 
select text from user_source where name=‘GET_EMPNAME‘;


刪除函數
drop function get_empname;

 

 

http://www.cnblogs.com/1848/articles/1828927.html

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.