0

i have written a ecpg code. i'm trying to insert a tuple into the table based on if the tuple already exists in the table. i'm getting "error: break statement not within loop or switch " on compiling please help i have commented in the code the line which the compiler showing the error

EXEC SQL DECLARE cursor4 CURSOR FOR
select count(*)
from works_on
where pno = :project
and essn = :ssn;

EXEC SQL OPEN cursor4;
EXEC SQL WHENEVER NOT FOUND DO BREAK;

while (SQLCODE==0)
{
EXEC SQL FETCH IN cursor4 INTO :cnt; 
}

EXEC SQL CLOSE cursor4;
EXEC SQL BEGIN DECLARE SECTION;
const char *qry = "INSERT INTO WORKS_ON VALUES(?,?,?);";
EXEC SQL END DECLARE SECTION;

if (cnt == 0 )
{
EXEC SQL PREPARE mystmt from :qry;
exec sql execute mystmt using '123456789',3,5.0; // where i am getting error

}
mu is too short
  • 426,620
  • 70
  • 833
  • 800
krrish0690
  • 51
  • 2
  • 11

1 Answers1

0

The WHENEVER sentence is executed until it found another WHENEVER in the code. You must cancel the WHENVER NOT FOUND break after your bucle.

EXEC SQL DECLARE cursor4 CURSOR FOR
select count(*)
from works_on
where pno = :project
and essn = :ssn;

EXEC SQL OPEN cursor4;
EXEC SQL WHENEVER NOT FOUND DO BREAK;

while (SQLCODE==0)
{
EXEC SQL FETCH IN cursor4 INTO :cnt; 
}
EXEC SQL WHENEVER NOT FOUND CONTINUE;   //Add this line to your code.

EXEC SQL CLOSE cursor4;
EXEC SQL BEGIN DECLARE SECTION;
const char *qry = "INSERT INTO WORKS_ON VALUES(?,?,?);";
EXEC SQL END DECLARE SECTION;

if (cnt == 0 )
{
EXEC SQL PREPARE mystmt from :qry;
exec sql execute mystmt using '123456789',3,5.0;