Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4619236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:23:48+00:00 2026-05-22T02:23:48+00:00

Requirement The table XYZ contains the column p_type which contains the column name of

  • 0

Requirement

The table XYZ contains the column p_type which contains the column name of PQR table . Now here I am fetching a record from the XYZ table and sum(amount) on the basis of the group by p_type which is fetched from the record from table XYZ. I have used partition by because I also want t_id and not in group by. Now I take the records fetched from PQR table and compare with a record of the XYZ table, and for each record fetched from PQR table, sum(amount) is compared with a record of XYZ table.

Question

I have fetched the xyz_id and t_id in the nested table, but I want to take this in cursor, IS there any way to do this so that I can select these record in one go.

The probable solution I found is to take a temporary table, but if I dont want to use a temp table, then what will be the solution.


DECLARE

type queryCursor is ref cursor;
v_ref queryCursor;

v_query queryCursor;
v_t_id   pqr.t_id%TYPE;
v_total         NUMBER (38);

CURSOR varcursor
IS
SELECT abc_id,p_type,p_amount
    FROM xyz;

v_temp          VARCHAR2 (4000);
v_temp1         VARCHAR2 (200);
v_temp2         VARCHAR2 (200);

TYPE XYZ_table IS TABLE OF  XYZ%ROWTYPE;
v_XYZ XYZ_table :=XYZ_table();
TYPE PQR_table IS TABLE OF PQR%ROWTYPE;
v_PQR PQR_table :=XYZ_table();
BEGIN

FOR varcursor_rec IN varcursor
LOOP
v_temp1 := varcursor_rec.p_type; --p_type contains column name of the pqr table
v_temp :=
        'SELECT T_ID,SUM(AMOUNT) OVER (PARTITION BY '
     || v_temp1
     || ' ORDER BY '
     || v_temp1
     || ') total from pqr
     ||';
 
 OPEN v_ref FOR v_temp;
  LOOP
FETCH V_REF into v_t_id,v_total;
exit when V_REF%notfound;
IF varcursor_rec.p_amount <v_total
THEN
counter:=counter+1;
v_XYZ.EXTEND;
v_PQR.EXTEND;
v_XYZ(counter).xyz_id:=varcursor_rec.xyz_id;
v_PQR(counter).t_id:=varcursor_rec.t_id;
      
 --HOW CAN I FETCH THE 
      
END IF;
END LOOP;
END LOOP;
END;
/

XYZ Table:

xyz_id                p_type          p_amount
============================================================
p_1                 p_id                 100000
p_2             p_id,t_status            100    

PQR Table:

T_id            p_id    amount    t_status
=================================================
1               E1      100       open
2               E2      200       open
3               E1      200       close
4               E2      300       open
5               E1      100       close
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:23:49+00:00Added an answer on May 22, 2026 at 2:23 am

    You can do this by using declared object types and tables of declared object types.

    Let’s start by creating the tables and data:

    CREATE TABLE xyz
    ( xyz_id VARCHAR2(3)
    , p_type VARCHAR2(20)
    , p_amount NUMBER
    );
    
    INSERT INTO xyz VALUES ('p_1','p_id','100000');
    INSERT INTO xyz VALUES ('p_2','p_id,t_status','100');
    
    CREATE TABLE pqr
    ( t_id NUMBER
    , p_id VARCHAR2(2)
    , amount NUMBER
    , t_status VARCHAR2(5)
    );
    
    INSERT INTO pqr VALUES (1,'E1',100,'open');
    INSERT INTO pqr VALUES (2,'E2',200,'open');
    INSERT INTO pqr VALUES (3,'E1',200,'close');
    INSERT INTO pqr VALUES (4,'E2',300,'open');
    INSERT INTO pqr VALUES (5,'E1',100,'close');
    

    Next, create the declared object types:

    CREATE TYPE pqr_tid_sumamt_rec IS OBJECT
    ( t_id NUMBER
    , sum_amount NUMBER
    );
    
    CREATE TYPE pqr_tid_sumamt_tab IS TABLE OF pqr_tid_sumamt_rec;
    
    CREATE TYPE xyztab_ptstab_rec IS OBJECT
    ( xyz_id VARCHAR2(3)
    , p_type VARCHAR2(20)
    , p_amount NUMBER
    , p_pqr_tid_sumamt_tab pqr_tid_sumamt_tab
    );
    
    CREATE TYPE xyztab_ptstab_tab AS TABLE OF xyztab_ptstab_rec;
    

    Now, we’ll create the function that will return a sys_refcursor with a nested table:

    CREATE OR REPLACE
    FUNCTION xyz_pqr_refcursor 
        RETURN sys_refcursor
    IS
        TYPE querycursor IS REF CURSOR;
        v_ref querycursor;
        v_query querycursor;
        v_t_id   pqr.t_id%TYPE;
        v_total         NUMBER (38);
        CURSOR varcursor IS 
            SELECT xyz_id,p_type,p_amount    
            FROM xyz;
        v_temp          VARCHAR2 (4000);
        v_temp1         VARCHAR2 (200);
        tab_pqr_tid_sumamt pqr_tid_sumamt_tab := pqr_tid_sumamt_tab();
        tab_xyztab_ptstab  xyztab_ptstab_tab  := xyztab_ptstab_tab();
        ret_cur sys_refcursor;
    BEGIN
        FOR varcursor_rec IN varcursor
        loop
            v_temp1 := varcursor_rec.p_type; --p_type contains column name of the pqr TABLE
            v_temp :=
                'SELECT T_ID,SUM(AMOUNT) OVER (PARTITION BY '     
                || v_temp1     
                || ' ORDER BY '     
                || v_temp1     
                || ') total from pqr';
    --        dbms_output.put_line('v_temp: '||v_temp);
            OPEN v_ref FOR v_temp;  
            loop
                fetch v_ref INTO v_t_id,v_total;
                exit WHEN v_ref%notfound;
    
                IF varcursor_rec.p_amount < v_total THEN
                    tab_pqr_tid_sumamt.EXTEND;
                    -- cast the cursor values into the record type to be added to the table type
                    tab_pqr_tid_sumamt( tab_pqr_tid_sumamt.count ) := pqr_tid_sumamt_rec(v_t_id, v_total);
                END IF;
            END loop;
            tab_xyztab_ptstab.EXTEND;
            -- cast the cursor values and nested table type to the record type and add to the table type
            tab_xyztab_ptstab(tab_xyztab_ptstab.count) := xyztab_ptstab_rec (varcursor_rec.xyz_id, varcursor_rec.p_type, varcursor_rec.p_amount, tab_pqr_tid_sumamt);
        END loop;
    
        --open the return refcursor
        OPEN ret_cur FOR 
            SELECT xyztab_ptstab_rec(xyz_id, p_type, p_amount, p_pqr_tid_sumamt_tab) FROM TABLE( tab_xyztab_ptstab );
    
        -- return the refcursor
        RETURN ret_cur;
    END xyz_pqr_refcursor;
    

    Now, let’s test the function:

    DECLARE
        test_cur      sys_refcursor;
        test_rec      xyztab_ptstab_rec;
        test_pqrtab   pqr_tid_sumamt_tab;
        counter       int := 1;
    BEGIN
        test_cur := xyz_pqr_refcursor();
        loop
            fetch test_cur INTO test_rec;
            exit WHEN test_cur%notfound;
            dbms_output.put_line('test_rec('||counter||') -- xyz_id: '||test_rec.xyz_id||', p_type: '||test_rec.p_type||', p_amount: '||test_rec.p_amount);
            test_pqrtab := test_rec.p_pqr_tid_sumamt_tab;
            FOR j IN 1 .. test_pqrtab.count
            loop
                dbms_output.put_line('.... test_pqrtab('||j||') - t_id: '||test_pqrtab(j).t_id||', sumamt: '||test_pqrtab(j).sum_amount);
            END loop;
            counter := counter + 1;
        END loop;
    EXCEPTION 
        WHEN others THEN dbms_output.put_line('whoops: '||sqlerrm);
    END;
    

    Test results:

    test_rec(1) -- xyz_id: p_1, p_type: p_id, p_amount: 100000
    test_rec(2) -- xyz_id: p_2, p_type: p_id,t_status, p_amount: 100
    .... test_pqrtab(1) - t_id: 3, sumamt: 300
    .... test_pqrtab(2) - t_id: 5, sumamt: 300
    .... test_pqrtab(3) - t_id: 4, sumamt: 500
    .... test_pqrtab(4) - t_id: 2, sumamt: 500
    

    Alternatively, you could just return the declared table type itself instead of a sys_refcursor.

    I hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following requirement. I have a table with a column that contains
Slightly unusual requirement here, which unfortunately is down to a poor table design a
My requirement like this: I have to fetch a row from table A. Now
We have a requirement to populate a master table which consists of columns from
I have a requirement where I have to drop one row from a table
I have requirement like, suppose I have a 'property' table which has 'ListingKey' field
I'm populating a CheckBoxList from database table. As per requirement, all the values should
My requirement is to create temp table then exec a store procedure which will
I have a requirement to accept an array of checked items from a table
The requirement is to copy rows from Table B into Table A. Only rows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.