0

我正在 db2 上创建一个过程,仅当表为空时才会将值插入表中。我创建了以下语句,但是由于出现错误,因此出现了问题:

[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;

谢谢!

4

2 回答 2

0

显然,这个小改动有助于解决问题:

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;
于 2016-12-05T08:05:51.340 回答
0

为什么你不这样做

create or REPLACE PROCEDURE proc1
BEGIN

DELETE FROM table1;

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

END;
于 2016-12-06T06:48:16.060 回答