Oracle Ref游标的输出结果

时间:2015-01-03 06:43:46

标签: oracle plsql oracle-sqldeveloper

如何在pl / sql块下面重写以避免硬编码列名逐一?以下数据来自OE架构。对于订单表,此表上有8列。是否可以在没有硬代码列名的情况下输出结果?任何帮助表示赞赏。

create or replace PACKAGE show_details AS
TYPE rt_order IS REF CURSOR RETURN orders%ROWTYPE;
TYPE typ_cust_rec IS RECORD
(cust_id NUMBER(6), cust_name VARCHAR2(20),
custphone customers.phone_numbers%TYPE,
credit NUMBER(9,2), cust_email VARCHAR2(30));
TYPE rt_cust IS REF CURSOR RETURN typ_cust_rec;
--Get order detail
PROCEDURE get_order(p_orderid IN NUMBER, p_cv_order IN OUT rt_order);
--Get customer detail
PROCEDURE get_cust(p_custid IN NUMBER, p_cv_cust IN OUT rt_cust);
END show_details;

create or replace PACKAGE BODY show_details 
AS 
PROCEDURE get_order (p_orderid IN NUMBER, p_cv_order IN OUT rt_order)
IS
BEGIN
OPEN p_cv_order FOR
SELECT * FROM orders
WHERE order_id = p_orderid;
-- CLOSE p_cv_order
END get_order;
PROCEDURE get_cust (p_custid IN NUMBER, p_cv_cust IN OUT rt_cust)
IS
BEGIN
OPEN p_cv_cust FOR
SELECT customer_id, cust_first_name,phone_numbers,
credit_limit,cust_email FROM customers WHERE customer_id = p_custid;
-- CLOSE p_cv_cust
END get_cust;
END;

SET SERVEROUTPUT ON SIZE UNLIMITED;
declare
cur_orders show_details.rt_order;
v_ordertab cur_orders%ROWTYPE;
begin
show_details.get_order(p_orderid =>2397, p_cv_order =>cur_orders);
LOOP
FETCH cur_orders INTO v_ordertab;
EXIT WHEN cur_orders%NOTFOUND;
DBMS_OUTPUT.PUT_LINE('ORDER_ID: ' || v_ordertab.ORDER_ID || ' ORDER_DATE: ' ||        v_ordertab.ORDER_DATE || ' ORDER_MODE: ' || v_ordertab.ORDER_MODE || ' CUSTOMER_ID: ' || v_ordertab.CUSTOMER_ID);
END LOOP;
exception
when others then
DBMS_OUTPUT.put_line ('Error Code : ' || SQLCODE);
end;
/

1 个答案:

答案 0 :(得分:0)

由于您正在从SQL Developer执行此操作,因此您可以使用从{* p>继承的variableprint命令:

variable cur_orders refcursor;
exec show_details.get_order(p_orderid => 2397, p_cv_order => :cur_orders);
print cur_orders

CUR_ORDERS
-------------------------------------------------------------------------------------------------------------------------------------------------
ORDER_ID                                CUSTOMER_ID                             ORDER_MODE                              ORDER_DATE                
--------------------------------------- --------------------------------------- --------------------------------------- ------------------------- 
2397                                    42                                      0                                       03-JAN-15                 

请注意,您将ref cursor变量作为绑定变量传递,因此在调用中的名称前面有一个冒号(:cur_order)。您需要run script而不是run statement

exec只是匿名阻止的简写,所以如果你愿意,你可以明确地做,但效果是一样的:

var cur_orders refcursor;
begin
  show_details.get_order(p_orderid => 2397, p_cv_order => :cur_orders);
end;
/
print cur_orders

如果您愿意,也可以在结果网格中获取输出as shown here,但print更接近您的dbms_output版本。或者你可以有一个包装函数,这样你就可以从普通的SQL调用中调用查询过程;取决于你的最终目标是什么,如果你只是手动执行程序来检查输出,那么print也可能足够好。