Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for loop enclosing PL/SQL blocks in pro*C

I have seen many pro*C programs, using a for loop to execute a set a statements "only once". For example,

for(i = 0; i < 1; i++)
{
    EXEC SQL EXECUTE
    DECLARE

        /* some declarations here */

    BEGIN

        /* some PL/SQL code here  */

    END-EXEC;
}

Why is this for loop necessary ?

like image 294
DJ Vishnu Teja Avatar asked Jul 15 '26 12:07

DJ Vishnu Teja


1 Answers

Just a wild guess: using such a loop might somehow simplify error handling when using WHENEVER DO BREAK or WHENEVER DO GOTO:

Consider the following code fragment:

for(i = 0; i < 1; i++)
{
    EXEC SQL WHENEVER SQLERROR DO BREAK; 
    EXEC SQL UPDATE emp SET sal = sal * 10; 
}
printf("%d",i);

If I'm not too wrong (don't have pro*C at hand right now), this would print 1 if the SQL query has completed without error. But 0 otherwise as we break before incrementing i.


Somewhat at the margin of that, there is a common idiom using an infinite for loop and a WHENEVER DO BREAK statement to fetch results:

EXEC SQL WHENEVER NOT FOUND DO break;
for (;;)
{
    EXEC SQL FETCH...
}
EXEC SQL CLOSE my_cursor; 
like image 72
Sylvain Leroux Avatar answered Jul 18 '26 01:07

Sylvain Leroux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!