简体   繁体   中英

Multiple Result Sets in Oracle Anonymous Block

Are there any Oracle tools that will output multiple result sets from an anonymous block?

In Sql Server I frequently run sets of statements similar to the following...

DECLARE @MostRecentPurchaseOrderId int;
SELECT @MostRecentPurchaseOrderId = MAX(PurchaseOrderId) FROM dbo.PurchaseOrders;
SELECT * FROM dbo.PurchaseOrders WHERE PurchaseOrderId = @MostRecentPurchaseOrderId;
SELECT * FROM dbo.PurchaseOrderDetails WHERE PurchaseOrderId = @MostRecentPurchaseOrderId;

I run some freshly-written c# then run these sql statements in ssms to have a look at the data I just wrote.

But in Oracle, everything is different. If I just wanted to run two sql statements, most tools will allow that. But if I want to declare a variable, select a value into it, then use the value of that variable in one or more select statements, I have to use an anonymous block. And you can't just have a stand-alone SELECT statement in an anonymous block. Here is what I have to do.

DECLARE purchaseOrderId NUMBER(16);
        TYPE RefCursor IS REF CURSOR;
        purchaseOrders RefCursor;
        purchaseOrderDetails RefCursor;
BEGIN
    SELECT MAX(PurchaseOrderId) INTO purchaseOrderId FROM PurchaseOrders;

    OPEN purchaseOrders FOR
        SELECT * FROM PurchaseOrders WHERE PURCHASE_ORDER_ID = purchaseOrderId;

    OPEN purchaseOrderDetails FOR
        SELECT * FROM PurchaseOrderDetails WHERE PURCHASE_ORDER_ID = purchaseOrderId;
END;

The question is how to display the results of the two ref cursors in a grid.

In TOAD, if I add a couple of undeclared variables (:purchaseOrdersOutput and :purchaseOrderDetailsOutput) it gets me almost there but not all the way. When I run it TOAD will display a dialog asking me to select the type of the two variables, I select Cursor and TOAD will execute and load the content of the first ref cursor into the data grid. But there is no output shown from the second cursor.

DECLARE purchaseOrderId NUMBER(16);
        TYPE RefCursor IS REF CURSOR;
        purchaseOrders RefCursor;
        purchaseOrderDetails RefCursor;
BEGIN
    SELECT MAX(PurchaseOrderId) INTO purchaseOrderId FROM PurchaseOrders;

    OPEN purchaseOrders FOR
        SELECT * FROM PurchaseOrders WHERE PURCHASE_ORDER_ID = purchaseOrderId;
    :purchaseOrdersOutput := purchaseOrders;

    OPEN purchaseOrderDetails FOR
        SELECT * FROM PurchaseOrderDetails WHERE PURCHASE_ORDER_ID = purchaseOrderId;
    :purchaseOrderDetailsOutput := purchaseOrderDetails;
END;

Any ideas?

As i understand from your requirement, you need to get the data sets of purchaseOrderDetails and PurchaseOrders filtered by the Maximum PurchaseOrderId , then display them together in a Grid-Like View.

You can't directly display results of a cursor in a PL/SQL block. Unlike SQL, PL/SQL is not designed to directly show results of a query in the IDE.

What you can do is to just outright write a SQL Statement using the below query and execute:

SELECT  po.* 
    ,   pod.*
FROM    PurchaseOrders po
    ,   PurchaseOrderDetails pod
WHERE   po.PURCHASE_ORDER_ID = pod.PURCHASE_ORDER_ID
and     po.PURCHASE_ORDER_ID = (SELECT  MAX(PurchaseOrderId) 
                                from    PurchaseOrders);

This requires no PL/SQL, only pure SQL and addresses your requirement (assuming i understood your requirement correctly).

One more approach you can take is to place the results of your query into variables and display them on the terminal using DBMS_OUTPUT .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM