[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Count number of rows in a cursor

Posted on 2007-10-05
8
Medium Priority
?
12,218 Views
Last Modified: 2012-06-27
Hi,

I am new to Procedures and cursors, so would appreciate some advice on this.

In the following Procedure, the Select of the Cursor can return one or more rows.

CREATE OR REPLACE PROCEDURE COMPUTE_DELAY
( P_STEP_ID IN BPM_DEF_STEPS.ID%TYPE)
AS
  xPRED_ID         BPM_DEF_STEPS.ID%TYPE;
  xSUCC_ID         BPM_DEF_STEPS.ID%TYPE;

  CURSOR c_get_parent(pSTEP_ID BPM_DEF_STEPS.ID%TYPE) IS
    SELECT
      PREDSTEP.ID                                    PRED_ID,
      SUCCSTEP.ID                                   SUCC_ID
    FROM
       BPM_DEF_STEPS                          PREDSTEP,
       BPM_DEF_STEPS                                 SUCCSTEP,
       BPM_DEF_STEP_CONDITIONS        COND,
       BPM_DEF_STEP_TRANSITIONS      TRANS
    WHERE
        PREDSTEP.ID                                   = COND.STEP_ID
        AND COND.ID                                  = TRANS.STEP_CONDITION_ID
        AND TRANS.NEXT_STEP_ID           = SUCCSTEP.ID
        AND SUCCSTEP.ID                          = pSTEP_ID
    ORDER BY
        PRED_ID,   SUCC_ID;

BEGIN
  OPEN c_get_parent(P_STEP_ID);
      LOOP
        FETCH c_get_parent INTO
          xPRED_ID,                        -- xPred_ID = ID of PRED
          xSUCC_ID;                        -- xSucc_ID = ID of SUCC
   
        &

        END LOOP;
    CLOSE c_get_parent;
END COMPUTE_DELAY;

Within the main Loop, I need to know how many rows were returned by the Select.  I know that c_get_parent%ROWCOUNT after Fetch, can tell how many rows have been retrieved so far, but I need to know how to determine the total number of rows in c_get_parent before I proceed with the next statements.
0
Comment
Question by:cgosney42
  • 5
  • 2
8 Comments
 
LVL 48

Expert Comment

by:schwertner
ID: 20022569
To do this you have to execute

v_count  integer;

.....
SELECT count(*) INTO v_count
FROM (
SELECT
      PREDSTEP.ID                                    PRED_ID,
      SUCCSTEP.ID                                   SUCC_ID
    FROM
       BPM_DEF_STEPS                          PREDSTEP,
       BPM_DEF_STEPS                                 SUCCSTEP,
       BPM_DEF_STEP_CONDITIONS        COND,
       BPM_DEF_STEP_TRANSITIONS      TRANS
    WHERE
        PREDSTEP.ID                                   = COND.STEP_ID
        AND COND.ID                                  = TRANS.STEP_CONDITION_ID
        AND TRANS.NEXT_STEP_ID           = SUCCSTEP.ID
        AND SUCCSTEP.ID                          = pSTEP_ID
    ORDER BY
        PRED_ID,   SUCC_ID);
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20022614
... or you can use the FOR LOOP with the same cursor as below : -

...
v_count := 0;
...
BEGIN
FOR i IN c_get_parent LOOP
       v_count := v_count + 1;
END LOOP;

OPEN ...
0
 
LVL 18

Accepted Solution

by:
Jinesh Kamdar earned 375 total points
ID: 20022628
Sorry, i missed the cursor arg. in the prev. post.

...
v_count := 0;
...
BEGIN
FOR i IN c_get_parent LOOP(p_step_id)
       v_count := v_count + 1;
END LOOP;

OPEN ...back to top
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:cgosney42
ID: 20022668
I was hoping for an easier solution to be honest.

Is it possible to fetch the values from the cursor twice?  Something like:

counter := 0;
FOR c IN c_get_parent
   LOOP
      FETCH c_get_parent INTO c ;
      EXIT WHEN c_get_parent%NOTFOUND ;
      counter := counter + 1 ;
END LOOP ;

Then to continue with ..

LOOP
        FETCH c_get_parent INTO
          xPRED_ID,                        -- xPred_ID = ID of PRED
          xSUCC_ID;                        -- xSucc_ID = ID of SUCC

?
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20022693
Yes, you can :) but you need not fetch it again in the LOOP, FOR LOOP does an implicit fetch. Use it the way i showed.
0
 

Author Comment

by:cgosney42
ID: 20022737
You were too quick for me ... you posted your response while I was typing mine LOL

Anyway, with a slight change of syntax, this works :)  Thanks

  xPredCount := 0;

  FOR i IN c_get_parent(p_step_id) LOOP
    xPredCount := xPredCount + 1;
  END LOOP;
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20022763
0
 
LVL 18

Expert Comment

by:Jinesh Kamdar
ID: 20022773
Oh sorry ... You were right ... Got it :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question