调用持久化包变量

时间:2013-04-29 12:01:12

标签: sql database oracle package

我已经创建了一个包和一个运行的包体,但我不确定如何测试持久包变量中的值。

这是包代码,它只是一个删除过程和创建函数,它们在包体中。

CREATE OR REPLACE PACKAGE customers AS
FUNCTION create_customer (Country VARCHAR2, First_Name VARCHAR2, Last_name VARCHAR2,
                    Birth_date VARCHAR2, Customer_Type VARCHAR2, Address VARCHAR2)
RETURN VARCHAR2;
PROCEDURE remove_customer (customer_id VARCHAR2);
FUNCTION count_customer RETURN NUMBER;
END customers;

现在这是包体

create or replace package body customers AS
total_customers NUMBER;
FUNCTION CREATE_CUSTOMER(        
       Country IN VARCHAR2 ,First_Name IN VARCHAR2 ,Last_Name IN VARCHAR2 ,Birth_Date IN VARCHAR2 ,Customer_Type IN VARCHAR2 ,Address IN VARCHAR2
) return VARCHAR2 IS
  new_customer_id VARCHAR2(8);
BEGIN
SELECT custid_seq.NEXTVAL
INTO new_customer_id
FROM DUAL;
INSERT INTO customer (Customer_id, Country, First_Name, Last_name, Birth_date, Customer_Type, Address)
VALUES (new_customer_id, Country, First_Name, Last_name, Birth_date, Customer_Type, Address);
total_customers := total_customers + 1;
RETURN (new_customer_id);
end;
PROCEDURE remove_customer (customer_id VARCHAR2) IS
BEGIN
DELETE FROM customer
WHERE customer.customer_id = remove_customer.customer_id;
total_customers := total_customers - 1;
END;
BEGIN
select count(*) into total_customers from customer;
END;

total_customers是我的持久性包变量,只有本地包这个包我只想知道如何测试以查看当前保存在变量中的值。

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

在您的包装规范中添加:

function get_total_customers return number;

在你的包裹体中添加:

function get_total_customers return number is
begin
    return total_customers;
end get_total_customers;

然后从SQL开始select customers.get_total_customers from dual

另一种方法是在规范中定义包变量而不是正文,但是它可以在外部和读取时进行更改。

无论哪种方式,该值只有意义within a session;如果你在一个会话中设置它然后在另一个会话中不可见。两者都可以计算,但如果你在一个会话中拨打create_customer(),另一个会报告旧的总数。

答案 1 :(得分:0)

将您的变量放入包规范中,进行测试。然后把它放回体内或留在规格中。保持简单......