2013-04-29 86 views
0

錯誤在命令開始位於第1行:語法錯誤在for循環

DECLARE 
    x NUMBER := 0; 
    counter NUMBER := 0; 
BEGIN 
    FOR i IN 1..4 LOOP 
     x := x + 1000; 
     counter := counter + 1; 
     INSERT INTO temp VALUES (x, counter, 'in OUTER loop'); 
     END; 
    END LOOP; 
    COMMIT; 
END; 

錯誤報告:

ORA-06550: line 11, column 10: 
PLS-00103: Encountered the symbol ";" when expecting one of the following: 

loop 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 
*Action: 
+0

你有'END','END LOOP','END'嵌套corect? – 2013-04-29 09:25:53

回答

2

第11行的第一END;不應該存在:

DECLARE 
    x NUMBER := 0; 
    counter NUMBER := 0; 
BEGIN 
    FOR i IN 1..4 LOOP 
     x := x + 1000; 
     counter := counter + 1; 
     INSERT INTO temp VALUES (x, counter, 'in OUTER loop'); 
    END LOOP; 
    COMMIT; 
END; 

你會o如果你有一個圍繞INSERT(比如說)的子塊,那麼很少需要它,例如用於特定的異常處理。

這也將是較好的一般在INSERT指定列名:

 INSERT INTO temp(col1, col2, col3) VALUES (x, counter, 'in OUTER loop'); 
4

你不需要PL/SQL此插入模式。以下工作在純SQL中:

create table temp(x number, counter number, text varchar2(20)); 

insert into temp 
select (rownum-1)*1000, rownum-1, 'in OUTER loop' 
from dual 
connect by level <=4;