0

I need to create a script that puts a key number from table A (which will be used as a parameter later), then flow that parameter or key number into a query and then dump those results into a holding record or table for later manipulation and such. Because each fetch has more than 1 row (in reality there are 6 rows per query results or per claim key) I decided to use the Bulk Collect clause. Though my initial test on a different database worked, I have not yet figured out why the real script is not working.

Here is the test script that I used:

DECLARE

--Cursors--
CURSOR prod_id is select distinct(product_id) from product order by 1 asc;

CURSOR cursorValue(p_product_id NUMBER) IS
        SELECT h.product_description,o.company_short_name
        FROM company o,product h
        WHERE o.product_id =h.product_id
        AND h.product_id =p_product_id
        AND h.product_id IS NOT NULL
        ORDER by 2;

    --Table to store Cursor data--        
    TYPE indx IS TABLE OF cursorValue%ROWTYPE
    INDEX BY PLS_INTEGER;
    indx_tab  indx;

    ---Variable objects---
    TotalIDs PLS_INTEGER;
    TotalRows PLS_INTEGER := 0 ;

BEGIN
      --PARAMETER CURSOR RUNS---
    FOR prod_id2 in prod_id LOOP
    dbms_output.put_line('Product ID: ' || prod_id2.product_id);
    TotalIDs := prod_id%ROWCOUNT;

          --FLOW PARAMETER TO SECOND CURSOR--
        Open cursorValue(prod_id2.product_id);
        Loop 
        Fetch cursorValue Bulk collect into indx_tab;

          ---data dump into table---
        --dbms_output.put_line('PROD Description: ' || indx_tab.product_description|| ' ' ||'Company Name'|| indx_tab.company_short_name);
        TotalRows := TotalRows + cursorValue%ROWCOUNT;
        EXIT WHEN cursorValue%NOTFOUND;
        End Loop;
        CLOSE cursorValue;
    End Loop;    
dbms_output.put_line('Product ID Total: ' || TotalIDs);
dbms_output.put_line('Description Rows: ' || TotalRows);
END;


Test Script Results:
anonymous block completed
Product ID: 1
Product ID: 2
Product ID: 3
Product ID: 4
Product ID: 5
Product ID Total: 5
Description Rows: 6

Update: Marking question as "answered" Thanks.

Asian Man
  • 25
  • 1
  • 2
  • 8
  • You're asking several questions, sort of, but I've answered what seems to be the most pressing. Since your test block works you already seem to know how to use bulk collect (though you seem to have removed a `loop`, maybe instead of commenting it out). I'm not sure if you have any further issues once the typo is fixed, but if so you should ask a separate question showing the current and desired behaviour of your real block. `dbms_output` should only really be used for basic debugging as clients may not show the output - not sure what you want to do with the data though. – Alex Poole Sep 15 '14 at 22:00
  • So made the typo correct (thanks for that I didn't initially see it). The following is what the error is now: Error report: ORA-06550: line 41, column 43: PLS-00302: component 'CLAIM_NUM' must be declared ORA-06550: line 41, column 11: PL/SQL: SQL Statement ignored 06550. 00000 - "line %s, column %s:\n%s" *Cause: Usually a PL/SQL compilation error. *Action: – Asian Man Sep 16 '14 at 15:51
  • Essentially, I want the rows (more than one) from the 2nd cursor bulk collected into a table so that case statements can reference the columns in the table. – Asian Man Sep 16 '14 at 15:52
  • Well, line 41 refers to `CUR_CLAIMNUM2.CLAIM_NUM`; but the cursor is selecting `CLAIM_NO`. It's exactly the same mistake. Sorry, but this is really basic debugging - look at the error, find the line number in the code, look for what the error text described... – Alex Poole Sep 16 '14 at 15:55
  • As suggested, I took a better look at the code and made several changes (also per the suggestions) without any errors. The question though remain, do I need to declare a record with the same %ROWTYPE as the 2nd cursor for the table to display columns? : – Asian Man Sep 16 '14 at 18:39
  • Don't completely change the question. The answer makes no sense now; and the new single-line question has no information or detail and is unanswerable. Please revert the question (there's a rollback button on the previous version). If the immediate problem you asked about (and the related second one that was also a typo) is resolved then accept the answer and ask a new question. – Alex Poole Sep 16 '14 at 18:42
  • I tried to do that but it isn't working. – Asian Man Sep 16 '14 at 19:05
  • I tried to add my updates but I am getting other errors. Also, I sent another flag message to the moderator about the errors I was getting. – Asian Man Sep 16 '14 at 19:45
  • Why do you keep removing the important parts of the question? – Alex Poole Sep 16 '14 at 20:43
  • I tried to chat you but some of the items shared was sensitive information so I removed it. I initially tried to modify it but I kept getting error messages. – Asian Man Sep 17 '14 at 15:53

1 Answers1

3

The first error is on line 7. On line 4 you have:

  CURSOR CUR_CLAIMNUM IS
                      SELECT DISTINCT(CLAIM_NO)FROM R7_OPENCLAIMS;

... and that seems to be valid, so your column name is CLAIM_NO. On line 7:

  CURSOR OPEN_CLAIMS (CLAIM_NUM  R7_OPENCLAIMS.CLAIM_NUM%TYPE)  IS

... so you've mistyped the column name as CLAIM_NUM, which doesn't exist in that table. Which is what the error message is telling you, really.

The other errors are because the cursor is invalid, becuase of that typo.

When you open the second cursor you have the same name confusion:

          OPEN OPEN_CLAIMS (CUR_CLAIMNUM2.CLAIM_NUM);

... which fails because the cursor is querying CLAIMNO not CLAIMNUM; except here it's further confused by the distinct. You haven't aliased the column name so Oracle applies one, which you could refer to, but it's simpler to add your own:

  CURSOR CUR_CLAIMNUM IS
                      SELECT DISTINCT(CLAIM_NO) AS CLAIM_NO FROM R7_OPENCLAIMS;

and then

          OPEN OPEN_CLAIMS (CUR_CLAIMNUM2.CLAIM_NO);

But I'd suggest you also change the cursor name from CUR_CLAIMNUM to CUR_CLAIM_NO, both in the definition and the loop declaration. And having the cursor iterator called CUR_CLAIMNUM2 is odd as it suggests that is itself a cursor name; maybe something like ROW_CLAIM_NO would be clearer.

Alex Poole
  • 183,384
  • 11
  • 179
  • 318