0

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!

Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
Janna Sherazi
  • 167
  • 1
  • 1
  • 15

2 Answers2

0

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;
Janna Sherazi
  • 167
  • 1
  • 1
  • 15
0

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;
Esperento57
  • 16,521
  • 3
  • 39
  • 45