PLSQL Insert in For LoopOracle insert loop - PLS-00103 errorHow to remove headers in database while importing the excel file through sql loaderNumber days in a Date RangeCopy and Cascade insert using PL/SQLCascade INSERT using SEQUENCE - ORACLEOracle SQL - Sum of distinct value belong year, for the first timeplsql - Oracle select with collectionsInserted Data into a table is not showing up when selected in oracleUsing PLSQL, how can I read rows with blob field that holds a text file with a million numbers and insert into another tableCreating a CSV file per Loop | PLSQL Oracle SQL Developer

Are there really no countries that protect Freedom of Speech as the United States does?

What should I do if actually I found a serious flaw in someone's PhD thesis and an article derived from that PhD thesis?

Can a Battle Master fighter with Extra Attack use the Commander's Strike maneuver before he throws a net?

Heyawake: An Introductory Puzzle

Number in overlapping range

Why aren't rockets built with truss structures inside their fuel & oxidizer tanks to increase structural strength?

Would the USA be eligible to join the European Union?

Is there a name for the technique in songs/poems, where the rhyming pattern primes the listener for a certain line, which never comes?

How can I shoot a bow using Strength instead of Dexterity?

How do I ask for 2-3 days per week remote work in a job interview?

Is there any official ruling on how characters go from 0th to 1st level in a class?

What if a restaurant suddenly cannot accept credit cards, and the customer has no cash?

How does the Moon's gravity affect Earth's oceans despite Earth's stronger gravitational pull?

Escape Velocity - Won't the orbital path just become larger with higher initial velocity?

What would cause a nuclear power plant to break down after 2000 years, but not sooner?

Are there any cons in using rounded corners for bar graphs?

Why won't the Republicans use a superdelegate system like the DNC in their nomination process?

Scam? Phone call from "Department of Social Security" asking me to call back

Go to last file in vim

What modifiers are added to the attack and damage rolls of this unique longbow from Waterdeep: Dragon Heist?

What is the opposite of "hunger level"?

How to get locks that are keyed alike?

How to measure if Scrum Master is making a difference and when to give up

What allows us to use imaginary numbers?



PLSQL Insert in For Loop


Oracle insert loop - PLS-00103 errorHow to remove headers in database while importing the excel file through sql loaderNumber days in a Date RangeCopy and Cascade insert using PL/SQLCascade INSERT using SEQUENCE - ORACLEOracle SQL - Sum of distinct value belong year, for the first timeplsql - Oracle select with collectionsInserted Data into a table is not showing up when selected in oracleUsing PLSQL, how can I read rows with blob field that holds a text file with a million numbers and insert into another tableCreating a CSV file per Loop | PLSQL Oracle SQL Developer






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I got an PL/SQL script to insert data from another table, but the result was not as i expected. The insert in BBB table only save the first row of d_rec loops. When i tried to print/output of d-Rec in another sql, it give me all the rows that saved in yyy table.



i already tried using cursor, but the result still the same.



DECLARE
GROUP_SEQ NUMBER;
TRANS_SEQ NUMBER;
ID NUMBER;
PP NUMBER := 1;
TRANS_ID NUMBER;
TRANS_SUM NUMBER;
UNIT VARCHAR(6);
DEBET NUMBER;
CREDIT NUMBER;

BEGIN
FOR v_rec in (SELECT unit from xxx)
LOOP
FOR d_rec in (SELECT ID, TRANS_SUM, UNIT from yyy where unit = v_rec.unit )
LOOP

UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'GROUP_SEQ'
RETURNING SEQ_COUNT
INTO GROUP_SEQ;
COMMIT;

INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
WHILE PP <= 4
LOOP
UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'TRANS_SEQ'
RETURNING SEQ_COUNT
INTO TRANS_SEQ;
COMMIT;

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;
PP := PP +1;
INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
PP :=1;
END LOOP;
END


The First insert give me all the rows in d_rec data. But the second insert only give me the first row of d_rec data.










share|improve this question


























  • Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

    – Alex Poole
    Mar 27 at 12:20












  • sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

    – gerhantara
    Mar 27 at 12:44











  • sorry, you are right. PP positioning that make only first row inserted... thx..

    – gerhantara
    Mar 27 at 12:56

















1















I got an PL/SQL script to insert data from another table, but the result was not as i expected. The insert in BBB table only save the first row of d_rec loops. When i tried to print/output of d-Rec in another sql, it give me all the rows that saved in yyy table.



i already tried using cursor, but the result still the same.



DECLARE
GROUP_SEQ NUMBER;
TRANS_SEQ NUMBER;
ID NUMBER;
PP NUMBER := 1;
TRANS_ID NUMBER;
TRANS_SUM NUMBER;
UNIT VARCHAR(6);
DEBET NUMBER;
CREDIT NUMBER;

BEGIN
FOR v_rec in (SELECT unit from xxx)
LOOP
FOR d_rec in (SELECT ID, TRANS_SUM, UNIT from yyy where unit = v_rec.unit )
LOOP

UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'GROUP_SEQ'
RETURNING SEQ_COUNT
INTO GROUP_SEQ;
COMMIT;

INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
WHILE PP <= 4
LOOP
UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'TRANS_SEQ'
RETURNING SEQ_COUNT
INTO TRANS_SEQ;
COMMIT;

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;
PP := PP +1;
INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
PP :=1;
END LOOP;
END


The First insert give me all the rows in d_rec data. But the second insert only give me the first row of d_rec data.










share|improve this question


























  • Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

    – Alex Poole
    Mar 27 at 12:20












  • sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

    – gerhantara
    Mar 27 at 12:44











  • sorry, you are right. PP positioning that make only first row inserted... thx..

    – gerhantara
    Mar 27 at 12:56













1












1








1








I got an PL/SQL script to insert data from another table, but the result was not as i expected. The insert in BBB table only save the first row of d_rec loops. When i tried to print/output of d-Rec in another sql, it give me all the rows that saved in yyy table.



i already tried using cursor, but the result still the same.



DECLARE
GROUP_SEQ NUMBER;
TRANS_SEQ NUMBER;
ID NUMBER;
PP NUMBER := 1;
TRANS_ID NUMBER;
TRANS_SUM NUMBER;
UNIT VARCHAR(6);
DEBET NUMBER;
CREDIT NUMBER;

BEGIN
FOR v_rec in (SELECT unit from xxx)
LOOP
FOR d_rec in (SELECT ID, TRANS_SUM, UNIT from yyy where unit = v_rec.unit )
LOOP

UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'GROUP_SEQ'
RETURNING SEQ_COUNT
INTO GROUP_SEQ;
COMMIT;

INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
WHILE PP <= 4
LOOP
UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'TRANS_SEQ'
RETURNING SEQ_COUNT
INTO TRANS_SEQ;
COMMIT;

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;
PP := PP +1;
INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
PP :=1;
END LOOP;
END


The First insert give me all the rows in d_rec data. But the second insert only give me the first row of d_rec data.










share|improve this question
















I got an PL/SQL script to insert data from another table, but the result was not as i expected. The insert in BBB table only save the first row of d_rec loops. When i tried to print/output of d-Rec in another sql, it give me all the rows that saved in yyy table.



i already tried using cursor, but the result still the same.



DECLARE
GROUP_SEQ NUMBER;
TRANS_SEQ NUMBER;
ID NUMBER;
PP NUMBER := 1;
TRANS_ID NUMBER;
TRANS_SUM NUMBER;
UNIT VARCHAR(6);
DEBET NUMBER;
CREDIT NUMBER;

BEGIN
FOR v_rec in (SELECT unit from xxx)
LOOP
FOR d_rec in (SELECT ID, TRANS_SUM, UNIT from yyy where unit = v_rec.unit )
LOOP

UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'GROUP_SEQ'
RETURNING SEQ_COUNT
INTO GROUP_SEQ;
COMMIT;

INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
WHILE PP <= 4
LOOP
UPDATE YYY_SEQUENCE
SET SEQ_COUNT = SEQ_COUNT + 1
WHERE SEQ_NAME = 'TRANS_SEQ'
RETURNING SEQ_COUNT
INTO TRANS_SEQ;
COMMIT;

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;
PP := PP +1;
INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
PP :=1;
END LOOP;
END


The First insert give me all the rows in d_rec data. But the second insert only give me the first row of d_rec data.







oracle plsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 12:41







gerhantara

















asked Mar 27 at 12:02









gerhantaragerhantara

103 bronze badges




103 bronze badges















  • Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

    – Alex Poole
    Mar 27 at 12:20












  • sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

    – gerhantara
    Mar 27 at 12:44











  • sorry, you are right. PP positioning that make only first row inserted... thx..

    – gerhantara
    Mar 27 at 12:56

















  • Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

    – Alex Poole
    Mar 27 at 12:20












  • sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

    – gerhantara
    Mar 27 at 12:44











  • sorry, you are right. PP positioning that make only first row inserted... thx..

    – gerhantara
    Mar 27 at 12:56
















Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

– Alex Poole
Mar 27 at 12:20






Wouldn't the innermost loop spin forever, as PP is never incremented? Although as KAS isn't defined this can't be the code you're actually running anyway... You may also want to reset PP back to one before that innermost loop, rather than after the d_rec loop - that might be why you only get a single insert, assuming there is only one unit from the outermost loop; but it isn't clear.

– Alex Poole
Mar 27 at 12:20














sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

– gerhantara
Mar 27 at 12:44





sorry, the code is only a snippet of the actual code. so there might be some errors. I have edited to adjust the code. hopefully it will be easier to understand.

– gerhantara
Mar 27 at 12:44













sorry, you are right. PP positioning that make only first row inserted... thx..

– gerhantara
Mar 27 at 12:56





sorry, you are right. PP positioning that make only first row inserted... thx..

– gerhantara
Mar 27 at 12:56












1 Answer
1






active

oldest

votes


















0














You are resetting the PP counter after the d_rec loop, inside the v_rec loop; which means that once it's been incremented to 4 for the first d_rec for a unit, subsequent d_rec iterations skip over the innermost loop - because PP <= 4 is never true for those.



A quick fix would be to move that reset in a loop:



...
END LOOP;
-- reset PP after this loop
PP :=1;
END LOOP;
-- instead of after this loop
--PP :=1;
END LOOP;
END


But it would probably be clear if you reset immediately before using it:



...
INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
-- reset here
PP := 1;
WHILE PP <= 4
LOOP
PP := PP +1;

...

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;

INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
END LOOP;
END


Or get rid of the explicit variable altogether - so it doesn't need to be declared, reset or incremented - and use a for loop instead:



...
INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
FOR PP IN 1..4
LOOP
...

IF (PP < 3) THEN
DEBET := TRANS_SUM;
CREDIT := 0;
ELSE
DEBET := 0;
CREDIT := TRANS_SUM;
END IF;

INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
COMMIT;
END LOOP;
END LOOP;
END LOOP;
END


Incidentally, all those commits look wrong - they make it impossible to restart on failure but also have a detrimental effect on performance. And you could probably simplify this to fewer loops.






share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376722%2fplsql-insert-in-for-loop%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You are resetting the PP counter after the d_rec loop, inside the v_rec loop; which means that once it's been incremented to 4 for the first d_rec for a unit, subsequent d_rec iterations skip over the innermost loop - because PP <= 4 is never true for those.



    A quick fix would be to move that reset in a loop:



    ...
    END LOOP;
    -- reset PP after this loop
    PP :=1;
    END LOOP;
    -- instead of after this loop
    --PP :=1;
    END LOOP;
    END


    But it would probably be clear if you reset immediately before using it:



    ...
    INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
    -- reset here
    PP := 1;
    WHILE PP <= 4
    LOOP
    PP := PP +1;

    ...

    IF (PP < 3) THEN
    DEBET := TRANS_SUM;
    CREDIT := 0;
    ELSE
    DEBET := 0;
    CREDIT := TRANS_SUM;
    END IF;

    INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
    COMMIT;
    END LOOP;
    END LOOP;
    END LOOP;
    END


    Or get rid of the explicit variable altogether - so it doesn't need to be declared, reset or incremented - and use a for loop instead:



    ...
    INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
    FOR PP IN 1..4
    LOOP
    ...

    IF (PP < 3) THEN
    DEBET := TRANS_SUM;
    CREDIT := 0;
    ELSE
    DEBET := 0;
    CREDIT := TRANS_SUM;
    END IF;

    INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
    COMMIT;
    END LOOP;
    END LOOP;
    END LOOP;
    END


    Incidentally, all those commits look wrong - they make it impossible to restart on failure but also have a detrimental effect on performance. And you could probably simplify this to fewer loops.






    share|improve this answer





























      0














      You are resetting the PP counter after the d_rec loop, inside the v_rec loop; which means that once it's been incremented to 4 for the first d_rec for a unit, subsequent d_rec iterations skip over the innermost loop - because PP <= 4 is never true for those.



      A quick fix would be to move that reset in a loop:



      ...
      END LOOP;
      -- reset PP after this loop
      PP :=1;
      END LOOP;
      -- instead of after this loop
      --PP :=1;
      END LOOP;
      END


      But it would probably be clear if you reset immediately before using it:



      ...
      INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
      -- reset here
      PP := 1;
      WHILE PP <= 4
      LOOP
      PP := PP +1;

      ...

      IF (PP < 3) THEN
      DEBET := TRANS_SUM;
      CREDIT := 0;
      ELSE
      DEBET := 0;
      CREDIT := TRANS_SUM;
      END IF;

      INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
      COMMIT;
      END LOOP;
      END LOOP;
      END LOOP;
      END


      Or get rid of the explicit variable altogether - so it doesn't need to be declared, reset or incremented - and use a for loop instead:



      ...
      INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
      FOR PP IN 1..4
      LOOP
      ...

      IF (PP < 3) THEN
      DEBET := TRANS_SUM;
      CREDIT := 0;
      ELSE
      DEBET := 0;
      CREDIT := TRANS_SUM;
      END IF;

      INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
      COMMIT;
      END LOOP;
      END LOOP;
      END LOOP;
      END


      Incidentally, all those commits look wrong - they make it impossible to restart on failure but also have a detrimental effect on performance. And you could probably simplify this to fewer loops.






      share|improve this answer



























        0












        0








        0







        You are resetting the PP counter after the d_rec loop, inside the v_rec loop; which means that once it's been incremented to 4 for the first d_rec for a unit, subsequent d_rec iterations skip over the innermost loop - because PP <= 4 is never true for those.



        A quick fix would be to move that reset in a loop:



        ...
        END LOOP;
        -- reset PP after this loop
        PP :=1;
        END LOOP;
        -- instead of after this loop
        --PP :=1;
        END LOOP;
        END


        But it would probably be clear if you reset immediately before using it:



        ...
        INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
        -- reset here
        PP := 1;
        WHILE PP <= 4
        LOOP
        PP := PP +1;

        ...

        IF (PP < 3) THEN
        DEBET := TRANS_SUM;
        CREDIT := 0;
        ELSE
        DEBET := 0;
        CREDIT := TRANS_SUM;
        END IF;

        INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
        COMMIT;
        END LOOP;
        END LOOP;
        END LOOP;
        END


        Or get rid of the explicit variable altogether - so it doesn't need to be declared, reset or incremented - and use a for loop instead:



        ...
        INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
        FOR PP IN 1..4
        LOOP
        ...

        IF (PP < 3) THEN
        DEBET := TRANS_SUM;
        CREDIT := 0;
        ELSE
        DEBET := 0;
        CREDIT := TRANS_SUM;
        END IF;

        INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
        COMMIT;
        END LOOP;
        END LOOP;
        END LOOP;
        END


        Incidentally, all those commits look wrong - they make it impossible to restart on failure but also have a detrimental effect on performance. And you could probably simplify this to fewer loops.






        share|improve this answer













        You are resetting the PP counter after the d_rec loop, inside the v_rec loop; which means that once it's been incremented to 4 for the first d_rec for a unit, subsequent d_rec iterations skip over the innermost loop - because PP <= 4 is never true for those.



        A quick fix would be to move that reset in a loop:



        ...
        END LOOP;
        -- reset PP after this loop
        PP :=1;
        END LOOP;
        -- instead of after this loop
        --PP :=1;
        END LOOP;
        END


        But it would probably be clear if you reset immediately before using it:



        ...
        INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
        -- reset here
        PP := 1;
        WHILE PP <= 4
        LOOP
        PP := PP +1;

        ...

        IF (PP < 3) THEN
        DEBET := TRANS_SUM;
        CREDIT := 0;
        ELSE
        DEBET := 0;
        CREDIT := TRANS_SUM;
        END IF;

        INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
        COMMIT;
        END LOOP;
        END LOOP;
        END LOOP;
        END


        Or get rid of the explicit variable altogether - so it doesn't need to be declared, reset or incremented - and use a for loop instead:



        ...
        INSERT INTO AAA (ID, SEQ) VALUES (ID, GROUP_SEQ);
        FOR PP IN 1..4
        LOOP
        ...

        IF (PP < 3) THEN
        DEBET := TRANS_SUM;
        CREDIT := 0;
        ELSE
        DEBET := 0;
        CREDIT := TRANS_SUM;
        END IF;

        INSERT INTO BBB(ID, SEQ, UNIT, DEBET, CREDIT) VALUES (ID, TRANS_SEQ, UNIT, DEBET, CREDIT);
        COMMIT;
        END LOOP;
        END LOOP;
        END LOOP;
        END


        Incidentally, all those commits look wrong - they make it impossible to restart on failure but also have a detrimental effect on performance. And you could probably simplify this to fewer loops.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 12:55









        Alex PooleAlex Poole

        141k7 gold badges117 silver badges198 bronze badges




        141k7 gold badges117 silver badges198 bronze badges





















            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







            Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55376722%2fplsql-insert-in-for-loop%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript