IF condition with FOR loop db2 plsql

617 views Asked by At

I'm creating a procedure on db2 that will insert values into a table only if the table is empty. I've created the following statements, but something is wrong since I'm getting error:

[42601][-104] An unexpected token "END-OF-STATEMENT" was found
following "END FOR". 
Expected tokens may include: " END IF".. SQLCODE=-104, SQLSTATE=42601,
DRIVER=4.7.85

create or REPLACE PROCEDURE proc1
BEGIN
IF (exists (select 1 from table1)) then
TRUNCATE TABLE table1;
ELSE
FOR l1 as
select id, max(bla) as bla from table2 group by id
do
insert into table1 (column1, column2)
values (id, bla);
END FOR;
END IF;
END;

thanks!

2

There are 2 answers

0
Janna Sherazi On

Apparently, this little change helped to solve the problem:

create or REPLACE PROCEDURE proc1
BEGIN
IF (exists (select 1 from table1)) then
DELETE FROM TABLE table1;
END IF;
FOR l1 as
select id, max(bla) as bla from table2 group by id
do
insert into table1 (column1, column2)
values (id, bla);
END FOR;
END;
0
Esperento57 On

why you dont do just this

create or REPLACE PROCEDURE proc1
BEGIN

DELETE FROM table1;

insert into table1 (column1, column2)
select id, max(bla) from table2 group by id;

END;