Joined: 22 Nov 2005 Posts: 700 Location: Troy, Michigan USA
To Add to ?Sandeep?s? post.
The normal (default) mode for PERFORMs is ?WITH TEST BEFORE? and works like this:
Test conditions, exit Perform loop if true
Go to top of Perform Loop
This is why, if you want to perform the loop 10 times, you would code
?VARYING SUB FROM 1 BY 1 UNTIL SUB > 10?
because the 10th time through the perform loop ?SUB? has a value of 10 when the condition is tested at the top of the Perform loop and you would exit after performing the loop only 9 times if you coded SUB = 10
The optional mode for PERFORMs is ?WITH TEST AFTER? and works like this:
Test condition, exit Perform loop if true
Go to top of perform Loop
In this case, if you want to perform the loop 10 times, you would code
?VARYING SUB FROM 1 BY 1 UNTIL SUB = 10?
because the test for conditions is done at the bottom of the perform loop, just before the variables are incremented and you have already executed the PERFORM loop where SUB = 10.
In the example you gave at the top, if SQLCODE = 000 at the start of the PERFORM loop, and you used the default (?WITH TEST BEFORE?), you could never execute your SQL, the condition would be true the first time through. With the optional ?WITH TEST AFTER? the test is done at the end of the PERFORM loop, and as ?Sandeep? stated your SQL is executed at least one time
Is PERFORM WITH TEST BEFORE EQUIVALENT TO PERFORM UNTIL OPTION, OR THE PERFORM WITH TEST BEFORE/AFTER NEEDS TO BE USED ALONG WITH UNTIL OPTION.
You need to specify the UNTIL condition also along with the PERFORM WITH TEST AFTER phrase.PERFORM WITH TEST BEFORE UNTIL 'condn' is equivalent to PERFORM UNTIL 'condn'.You should not omit the UNTIL phrase in either case.