0

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

这是包代码,它相当简单,只是包主体中的删除过程和创建函数。

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 是我的持久包变量,并且仅在此包中我只是想知道如何测试以查看当前保存在变量中的值。

谢谢您的帮助

4

2 回答 2

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

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

无论哪种方式,该值都只会在会话中有意义;如果您在一个会话中设置它,那么在另一个会话中将不可见。两者都可以计算,但是如果您create_customer()在一个会话中调用,另一个会话仍将报告旧的总数。

于 2013-04-29T12:16:35.603 回答
0

将您的变量放入包装规格中,进行测试。然后将其放回体内或留在规格中。把事情简单化...

于 2013-04-29T12:35:24.413 回答