0

Here is the code. Basically the ICRTASKREFERENCE table has 31 rows I need to loop in. So the loop should runs 31 times, but for now it only runs once. Can someone help me this? much appreciate!


create or replace PROCEDURE IC_CREATE_TASKS_P AS 
    startnumber number:=1;
    maxCount number;
    taskName varchar(255);
    BEGIN
        SELECT count(*) INTO maxCount from ICRTASKREFERENCE;
         WHILE startnumber <= maxCount
            LOOP
                SELECT TASKCODE INTO taskName from ICRTASKREFERENCE WHERE TASKREFERENCEID = startnumber;
                taskName := UPPER(taskName);
                BEGIN
                    EXECUTE IMMEDIATE 'INSERT INTO ictask (
                        STATUS,
                        CATEGORY,
                        TASKCODE,
                        TASKTYPE,
                        SOURCETABLECODE,
                        MODIFIEDON,
                        MODIFIEDBY,
                        CREATEDON,
                        PRIORITY
                    ) 
                    SELECT 
                        ''OPEN'',
                        taskReferenceTable.CATEGORY,
                        taskReferenceTable.TASKCODE,
                        taskReferenceTable.TASKTYPE,
                        taskReferenceTable.SOURCETABLECODE,
                        SYSDATE,
                        ''Administrator'',
                        SYSDATE,
                        taskReferenceTable.PRIORITY
                     FROM (SELECT * from '||taskName||' WHERE NEWFLAG=1) newFlag cross join (SELECT * from ICRTASKREFERENCE WHERE TASKREFERENCEID = '||startnumber||') taskReferenceTable WHERE newFlag.MSTID NOT IN 
                     (SELECT SOURCETABLEID FROM ICTASK)';

                END;

                     startnumber := startnumber + 1;
            END LOOP;
    END;
6
  • What proof do you have that loop executed only once? Hint: Insert into .. select do not insert anything if select returns no rows. Commented Mar 11, 2020 at 20:10
  • @Tejash I see the task table finally only has the result for first taskcode. The taskcode is from ICRTASKREFERENCE table Commented Mar 11, 2020 at 20:25
  • Loop runs as many times as there are rows in ICRTASKREFERENCE table. So, how many rows does it have? Commented Mar 11, 2020 at 20:30
  • do the tables named according to taskName really exist? Commented Mar 11, 2020 at 20:31
  • @Littlefoot the ICRTASKREFERENCE has 31 rows, so the loop should run 31 times Commented Mar 11, 2020 at 20:35

1 Answer 1

2

Simpler code would be this:

begin
  for cur_r in (SELECT rownum as startnumber,
                       TASKCODE as taskName 
                from ICRTASKREFERENCE
               ) loop
    execute immediate ... (your dynamic SQL goes here)
  end loop;
end;

Make sure that tables you use in dynamic SQL actually exist, as well as corresponding TASKREFERENCEID column value.

Sign up to request clarification or add additional context in comments.

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.