15. PL/SQL Package

PL/SQL Package是講邏輯相關的PL/SQL的變數和Procedure包裹在一起。

Procedure Package是由兩個強製性的部分組成:
  • Package 規格
  • Package Body

Package規格

規格主要是宣告的類型,變數,常數,異常,遊標和子程序可從封裝外部引用。換句話說,Package包含關於Package的內容的所有資訊,但不包含用於子程序的程式碼。
置於規格的所有物件被稱為公共物件。但任何子程序只要是在封裝主體沒有被Package定義為私有物件。
下面的程式碼顯示了具有單一的Procedure的Package規格定義。一個Package中可以定義的全局變量和多個Procedure或Function。
CREATE PACKAGE cust_sal AS
  PROCEDURE find_sal(c_id customers.id%type);
END cust_sal;
/
結果如下:
Package created.

Package Body

CREATE PACKAGE BODY語法用於建置Package Body。下面的程式碼顯示了Package Body宣告上面建置的cust_salPackage。在前面課程中,我們已經在資料庫中建置了一個CUSTOMERS的範例。


CREATE OR REPLACE PACKAGE BODY cust_sal AS
  PROCEDURE find_sal(c_id customers.id%TYPE) IS
  c_sal customers.salary%TYPE;
  BEGIN
     SELECT salary INTO c_sal
     FROM customers
     WHERE id = c_id;
     dbms_output.put_line('Salary: '|| c_sal);
  END find_sal;
END cust_sal;
/
結果如下:
Package body created.

使用Package Element

存取Package Element(變數,Procedure或Function)的語法如下:
Package_name.element_name;
我們已經在上面的建置的Package,下面的Procedure是使用cust_salPackage的find_sal方法:
DECLARE
  code customers.id%type := &cc_id;
BEGIN
  cust_sal.find_sal(code);
END;
/

當上面的程式碼在SQL提示符號下執行,提示輸入客戶ID,當輸入ID,會顯示相應的薪如下:
Enter value for cc_id: 1
Salary: 3000

PL/SQL procedure successfully completed.

範例:

下面的Procedure提供了一個更為完整的方案。
Select * from customers;

+----+----------+-----+-----------+----------+
| ID | NAME     | AGE | ADDRESS   | SALARY   |
+----+----------+-----+-----------+----------+
|  1 | Ramesh   |  32 | Ahmedabad |  3000.00 |
|  2 | Khilan   |  25 | Delhi     |  3000.00 |
|  3 | kaushik  |  23 | Kota      |  3000.00 |
|  4 | Chaitali |  25 | Mumbai    |  7500.00 |
|  5 | Hardik   |  27 | Bhopal    |  9500.00 |
|  6 | Komal    |  22 | MP        |  5500.00 |
+----+----------+-----+-----------+----------+

Package規範定義

CREATE OR REPLACE PACKAGE c_package AS
  -- Adds a customer
  PROCEDURE addCustomer(c_id   customers.id%type,
  c_name  customers.name%type,
  c_age  customers.age%type,
  c_addr customers.address%type,
  c_sal  customers.salary%type);
 
  -- Removes a customer
  PROCEDURE delCustomer(c_id  customers.id%TYPE);
  --Lists all customers
  PROCEDURE listCustomer;

END c_package;
/
結果如下:
Package created.

建置Package的主體部分:

CREATE OR REPLACE PACKAGE BODY c_package AS
  PROCEDURE addCustomer(c_id  customers.id%type,
     c_name customers.name%type,
     c_age  customers.age%type,
     c_addr  customers.address%type,
     c_sal   customers.salary%type)
  IS
  BEGIN
     INSERT INTO customers (id,name,age,address,salary)
        VALUES(c_id, c_name, c_age, c_addr, c_sal);
  END addCustomer;
 
  PROCEDURE delCustomer(c_id   customers.id%type) IS
  BEGIN
      DELETE FROM customers
        WHERE id = c_id;
  END delCustomer;

  PROCEDURE listCustomer IS
  CURSOR c_customers is
     SELECT  name FROM customers;
  TYPE c_list is TABLE OF customers.name%type;
  name_list c_list := c_list();
  counter integer :=0;
  BEGIN
     FOR n IN c_customers LOOP
     counter := counter +1;
     name_list.extend;
     name_list(counter)  := n.name;
     dbms_output.put_line('Customer(' ||counter|| ')'||name_list(counter));
     END LOOP;
  END listCustomer;
END c_package;
/

結果如下:
Package body created.

使用Package:

下面的Procedure使用宣告並在Packagec_package中定義方法。
DECLARE
  code customers.id%type:= 8;
BEGIN
     c_package.addcustomer(7, 'Rajnish', 25, 'Chennai', 3500);
     c_package.addcustomer(8, 'Subham', 32, 'Delhi', 7500);
     c_package.listcustomer;
     c_package.delcustomer(code);
     c_package.listcustomer;
END;
/
結果如下:
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik    
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish
Customer(8): Subham
Customer(1): Ramesh
Customer(2): Khilan
Customer(3): kaushik    
Customer(4): Chaitali
Customer(5): Hardik
Customer(6): Komal
Customer(7): Rajnish

PL/SQL procedure successfully completed

沒有留言:

張貼留言