使用FOR循环db2 plsql的IF条件

时间:2016-12-04 22:26:56

标签: if-statement for-loop db2 sql-pl

我正在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;

谢谢!

2 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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;