PL/SQL包是组逻辑相关的PL/SQL类型,变量和子程序模式对象。
程序包将有两个强制性的部分:
包装规范定义
包体或定义
规范是接口到包。它只是声明的类型,变量,常量,异常,游标和子程序可从封装外部引用。换句话说,它包含关于包的内容的所有信息,但不包括用于子程序的代码。
置于规范的所有对象被称为公共对象。任何子程序在封装主体中没有包定义但编码被称为私有对象。
下面的代码片段显示了具有单一的程序包规范定义。一个包中可以定义的全局变量和多个程序或函数。
CREATE PACKAGE cust_sal AS PROCEDURE find_sal(c_id customers.id%type); END cust_sal; /
当上述代码在SQL提示符执行时,它产生了以下结果:
Package created.
包体已经在包定义和其他私人声明中声明的各种方法,这是从代码隐藏在包外的代码。
CREATE PACKAGE BODY语句用于创建包体。下面的代码片段显示了包体声明上面创建的cust_sal包。
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; /当上述代码在SQL提示符执行时,它产生了以下结果:
Package body created.
访问包元素(变量,过程或函数)的语法如下:
package_name.element_name;想想,我们已经在上面的数据库模式中创建的包,下面的程序是使用cust_sal包的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.
下面的程序提供了一个更为完整的方案。我们将使用存储在数据库中的CUSTOMERS表的以下记录:
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 | +----+----------+-----+-----------+----------+
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; /当上面的代码在SQL提示符下执行,它会创建上述包,并显示以下结果:
Package created.
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; /上面的例子中使用嵌套表,我们将在下一章讨论。当上述代码在SQL提示符执行时,它产生了以下结果:
Package body created.
下面的程序使用声明并在包c_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; /当上述代码在SQL提示符执行时,它产生了以下结果:
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