函數 與 預存程序
Oracle 資料庫中不僅可以使用 單條語句對資料庫進行增、刪、改、 查 操作,而且可以多條語句組成一個i額語句塊, 並一起執行。
這些語句塊可以進行顯示命名, 並被其他應用調用。 這些命名的語句塊 被稱為 函數 與 預存程序。
Oracle 中的 自訂函數;
Oracle 中的 預存程序;
封裝函數 與 預存程序—程式包。
-- 函數
-- 函數是 Oracle 中 的常用對象之一, 與 其他編程 語言 函數 一樣, Oracle 中的函數也必須返回 一個值。 這也是函數 區別 於 預存程序的重要特徵。
-- 函數簡介:
1. 函數 與 功能的劃分
2. 函數 的 參數
3. 函數 的 傳回值
建立 函數
- --1. 建立函數
-
- --create or replace function get_hello_msg return varchar2 as
- --begin
- -- return 'hello world';
- --end get_hello_msg;
- --/
-
- SQL> create or replace function get_hello_msg
- return varchar2 as
- begin
- return 'hello world';
- end get_hello_msg;
- /
-
- 函數已建立。
-
-
-
-
- --2. 在資料字典 中 查看函數的資訊 select object_name, object_type, status from user_objects where lower(object_name)='get_hello_msg';
-
- SQL> set linesize 180;
- SQL> select object_name, object_type, status from user_objects where lower(object_name)='get_hello_msg';
-
- OBJECT_NAME OBJECT_TYPE STATUS
- ------------------------------------------------------------------------------------------------------------------------------------ --------------
- GET_HELLO_MSG FUNCTION VALID
-
-
-
- --3. 查看函數的傳回值
-
- --set serverout on;
- --declare msg varchar2(20);
- --begin
- -- msg := get_hello_msg;
- -- dbms_output_line(msg);
- --end;
- --/
-
- SQL> set serverout on;
- SQL> declare msg varchar2(20);
- begin msg := get_hello_msg; dbms_output.put_line(msg); end;
- /
-
- hello world
-
- PL/SQL 過程已成功完成。