我需要让用户输入值并使用pl / sql函数返回多个值,我设法只返回一个值并且无法提升用户输入值。我需要客户5将订单从30增加到200.允许用户输入:
1)customer_ID和的数字4 2)100更新数量。
并在更改之前和之后打印输出,这样的事情 ..
Enter value for customer_id: 4
old 7: l_customer_id :=&customer_ID;
new 7: l_customer_id :=4;
Enter value for new_quantity: 520
old 9: l_new_quantity :=&new_quantity;
new 9: l_new_quantity :=520;
Old quangtity: 100
New quantity: 520
Quantity changed from 100 to 520 (+420)
我的代码但只返回旧的数量值
CREATE OR REPLACE FUNCTION update_sales
(
p_customer_id IN sales.customer_id%type,
p_product_id IN sales.product_id%type,
p_new_quantity IN sales.quantity%type
)
RETURN sales.quantity%type
AS
l_old_quantity sales.quantity%type;
BEGIN
SELECT quantity
INTO l_old_quantity
FROM sales
WHERE customer_id = p_customer_id
AND product_id = p_product_id;
UPDATE sales
SET quantity = p_new_quantity
WHERE customer_id = p_customer_id
AND product_id = p_product_id;
RETURN l_old_quantity;
END;
/
DECLARE
l_old_quantity sales.quantity%type;
BEGIN
l_old_quantity := update_sales(5, 4, 200);
dbms_output.put_line('Quantity was ' || l_old_quantity);
END;
/
产品表
Create table sales (customer_ID number(10), product_ID number(10), quantity number(10));
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,1,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,2,34);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(1,3,654);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,7,32);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(4,3,23);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(3,3,111);
INSERT INTO sales (customer_ID, product_ID, quantity) Values(5,4,6);
请咨询
答案 0 :(得分:0)
通常,无法从函数返回多个变量。
但是,您有以下两种选择:
选项1: 不建议使用功能:
您可以在函数参数中使用OUT
类型:
CREATE OR REPLACE FUNCTION update_sales
(
p_customer_id_in IN sales.customer_id%type,
p_product_id_in IN sales.product_id%type,
p_new_quantity_in IN sales.quantity%type
p_customer_id_out OUT sales.customer_id%type,
p_product_id_out OUT sales.product_id%type,
p_old_quantity_out OUT sales.quantity%type
)
....
--The remaining of your function
对于customer_id
,如果您愿意,可以将IN OUT
类型用于一个变量。
然后当你打电话时,你会使用这样的东西:
v_result:=update_sales(p_customer_id_in => 5, p_product_id_in => 4, p_new_quantity_in => 200, p_customer_id_out => v1_out, p_product_id_out => v2_out, p_new_quantity_out => v3_out );
选项2:
使用record
类型
要做到这一点,首先需要定义类型包含要返回的三个变量,如下所示:
TYPE new_type is record(l_customer_id sales.customer_id%type, l_old_quantity sales.quantity%type, l_new_quantity sales.quantity%type);
然后你可以在你的函数中使用它,如下所示:
CREATE OR REPLACE FUNCTION update_sales
(
p_customer_id IN sales.customer_id%type,
p_product_id IN sales.product_id%type,
p_new_quantity IN sales.quantity%type
)
RETURN new_type AS
new_type_variable newtype;
....
BEGIN
--You can refer to the fields of the records using (.)
-- e.g., new_type_variable.l_customer_id
RETURN new_type_variable ;
END;
基于第一种方法的完整功能
CREATE OR REPLACE FUNCTION update_sales
(
p_customer_id_in IN sales.customer_id%type,
p_product_id_in IN sales.product_id%type,
p_new_quantity_in IN sales.quantity%type,
p_customer_id_out OUT sales.customer_id%type,
p_product_id_out OUT sales.product_id%type,
p_old_quantity_out OUT sales.quantity%type
)
RETURN sales.quantity%type
AS
l_old_quantity sales.quantity%type;
BEGIN
SELECT quantity
INTO p_old_quantity_out
FROM sales
WHERE customer_id = p_customer_id_in
AND product_id = p_product_id_in;
UPDATE sales
SET quantity = p_new_quantity_in
WHERE customer_id = p_customer_id_in
AND product_id = p_product_id_in;
RETURN l_old_quantity;
END;
/
测试
CREATE TABLE TEMP_TEST(a varchar2(100));
DECLARE
V_RESULT NUMBER;
v1_out sales.customer_id%type;
v2_out sales.product_id%type;
v3_out sales.quantity%type;
BEGIN
DBMS_OUTPUT.ENABLE(10000);
DBMS_OUTPUT.PUT_LINE('TEST');
v_result:=update_sales
(
p_customer_id_in => 5,
p_product_id_in => 4,
p_new_quantity_in => 200,
p_customer_id_out => v1_out,
p_product_id_out => v2_out,
p_old_quantity_out => v3_out
);
INSERT INTO TEMP_TEST VALUES ('p_customer_id_out = ' ||v1_out);
INSERT INTO TEMP_TEST VALUES ('p_product_id_out = '||v2_out);
INSERT INTO TEMP_TEST VALUES ('p_old_quantity_out = '||v3_out);
COMMIT;
END;
/
SELECT * FROM TEMP_TEST;
结果:
p_customer_id_out =
p_product_id_out =
p_old_quantity_out = 200
因为在您的功能中,您只为p_old_quantity_out
答案 1 :(得分:0)
在您的示例中,我实际上不明白为什么您需要返回新数量,因为这是您实际提交给函数的参数。
然而,我只是使用一个过程而不是一个函数,并根据需要返回尽可能多的OUT参数:
CREATE OR REPLACE FUNCTION update_sales
( p_customer_id IN sales.customer_id%type,
, p_product_id IN sales.product_id%type,
, p_new_quantity IN sales.quantity%type,
, p_old_quantity OUT sales.quantity%type
, p_transaction_date OUT DATE /* just as an example */
)
AS
BEGIN
SELECT quantity
INTO p_old_quantity
FROM sales
WHERE customer_id = p_customer_id
AND product_id = p_product_id;
UPDATE sales
SET quantity = p_new_quantity
WHERE customer_id = p_customer_id
AND product_id = p_product_id;
p_transaction_date := SYSDATE;
END;
/
DECLARE
l_old_quantity sales.quantity%type;
l_transaction_date DATE;
BEGIN
update_sales(5, 4, 200, l_old_quantity, l_current_date);
dbms_output.put_line('Quantity was ' || l_old_quantity);
dbms_output.put_line('Transaction date was ' || l_transaction_date);
END;
/