1

I have a PL/SQL statement that should iterate through a select statement, execute a stored procedure and then select something from a table.

The statement looks like the one below:

BEGIN
    FOR r IN (
        select org_id ,name from table2 order by 1
    ) LOOP

        dbms_application_info.set_cl (r.org_id);
        Select prj_id, prj_name 
        INTO l_output
        FROM table1
        WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY');   
        dbms_sql.return_result(l_output); --error     
    END LOOP;
END;

I don't know how to display all the rows returned by the query. Could someone try to help me figure this out? I searched for a solution but without any luck until now..

PS: Table1 is a view that needs to have the stored procedure initiated before the select

Thanks in advance!

3
  • What you mean by display result ? Do you want just print values from inner query? Commented May 7, 2018 at 10:29
  • Yup, that is what I want.. to print the values Commented May 7, 2018 at 10:30
  • Why is the select query inside the loop? I don't see any column being used in the query. Commented May 7, 2018 at 11:31

3 Answers 3

2

So maybe use the same construction as in outer query?

BEGIN
    FOR r IN (
        select org_id ,name from table2 order by 1
    ) LOOP

        dbms_application_info.set_cl (r.org_id);

        FOR inner_query IN (
          Select prj_id, prj_name 
          FROM table1
          WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY');   
        ) LOOP
          dbms_output.put_line('prj_id: ' || inner_query.prj_id || ' prj_name:' || inner_query.prj_name);
        END LOOP;  
    END LOOP;
END;

Edit: If you want implicitly return cursor from your procedure through dbms_sql.return_result you should declare variable as ref_cursor:

l_output SYS_REFCURSOR

And then open it for your query:

 OPEN l_output FOR SELECT ...

And finally return with following procedure:

 DBMS_SQL.RETURN_RESULT(l_output);
Sign up to request clarification or add additional context in comments.

8 Comments

I recommend to avoid structures like "Loop in the loop", cause its very resource demanding and often hard to read and maintain.
I did set the l_output as SYS_REFCURSOR but I have the following error: component 'RETURN_RESULT' must be declared
@Ychdziu I don't agree that loop in the loop is very resource demanding, as it always depends on what you want to achieve from. Anyway, OP just wants to print values from the query based on the element of the collection, so beyond loop in the loop where he can use an aggregate function which can be harder to read than this.
@rosuandreimihai what's your database version? RETURN_RESULT is available from 12c onward
@rosuandreimihai just print it as in my answer using dbms_output.put_line
|
2

Procedure dbms_sql.return_result accepts/returns a ref cursor, you need to open the ref cursor before calling this procedure:

    DECLARE
      l_output sys_refcursor;    
    BEGIN
      FOR r IN (SELECT org_id,
                       NAME
                  FROM table2
                 ORDER BY 1) LOOP

        dbms_application_info.set_cl(r.org_id);
        OPEN l_output FOR
          SELECT prj_id,
                 prj_name
            FROM table1
           WHERE creation_date > to_date('10/01/2017', 'DD-MM-YYYY');
        dbms_sql.return_result(l_output);
      END LOOP;
    END;

5 Comments

Hi, the error returned is: identifier 'L_OUTPUT' must be declared
Hi, thank you! But still.. the error that I receive is: component 'RETURN_RESULT' must be declared
If you are still getting this error, then I doubt that you do not have access on package 'DBMS_SQL', ask your admin for execute privilege on this package.
If he wouldn't have access to DBMS_SQL then the error would be: component 'DBMS_SQL' must be declared
Found out, that dbms_sql.return_result is in DB version 12c and up. In older versions - there are no info about it.
0
BEGIN
    FOR r IN (select org_id ,name from table2 order by 1)
       LOOP
          dbms_application_info.set_cl (r.org_id);
         for x in (Select prj_id, prj_name
                    FROM table1
                   WHERE CREATION_DATE>TO_DATE('10/01/2017','DD-MM-YYYY'))
            loop   
              dbms_output.put_line('prj_id:  '||x.prj_id ||'  prj_name:  '||x.prj_name );
         end loop;     
    END LOOP;
END;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.