Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

FOR LOOP with EXECUTE IMMEDIATE

530728Jan 23 2009 — edited Jan 26 2009
I'm trying to figure out how to loop through a recordset returned from
an EXECUTE IMMEDIATE statement. I created a package in a "master"
schema that is configured to act on the tables in the calling schema
when invoked. None of those tables exist in the master schema, so all
of the SQL in the package must be invoked using EXECUTE IMMEDIATE,
otherwise I can't compile the package. My problem is that I can't quite
figure out how to do something like the following with EXECUTE
IMMEDIATE:

{color:#999999}FOR rec IN (SELECT id, COUNT(DISTINCT name) AS dup_cnt FROM item_tbl GROUP BY id, HAVING COUNT(DISTINCT name) > 1)
LOOP
NULL; --do something here
END LOOP;{color}

I tried:

{color:#999999}...
AS
TYPE ItemRec IS RECORD (
item_id NUMBER,
dup_cnt NUMBER);
TYPE ItemSet IS TABLE OF ItemRec;
dup_items ItemSet;
BEGIN

EXECUTE
IMMEDIATE 'SELECT id, COUNT(DISTINCT name) dup_cnt FROM item_tbl GROUP
BY id, HAVING COUNT(DISTINCT name) > ' INTO dup_items;

FOR i IN dup_items.FIRST..dup_items.LAST
LOOP
NULL; --do something here
END LOOP;
...{color}


This gives me a "PLS-00597: expression 'DUP_ITEMS' in the INTO list is of wrong type"

Can someone tell me what I'm doing wrong?

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 23 2009
Added on Jan 23 2009
2 comments
23,432 views