2016-12-04 130 views
0

我在db2上創建一個過程,只有當表爲空時纔會將值插入到表中。我創建了下面的語句,但什麼是錯的,因爲我得到的錯誤:如果條件與FOR循環db2 plsql

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

的感謝!

回答

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