ORACLE - 程序中的错误PLS-00103

时间:2016-06-07 14:03:25

标签: sql database oracle stored-procedures plsql

我在ORACLE文档之后编写了这个简单的过程,以便用PL / SQL进行练习:

create procedure BADGE_PROCEDURE (badges NUMBER) AS  
  loops NUMBER;  
BEGIN  
  loops := badges;  
  loops := loops - 1;  
  BEGIN LOOP  
    insert into BADGE values (BADGE_ID_SEQUENCE.nextval, 'prova', 'prova', '01-JAN-01');       
    loops := loops - 1;  
    EXIT WHEN loops < 0;  
  END LOOP;  
END;  
/

这是我的BADGE表:

CREATE TABLE BADGE  
(badge_id NUMBER PRIMARY KEY,   
name VARCHAR(20),   
surname VARCHAR(20) NOT NULL,   
birthday DATE);

最后这是我的序列:

CREATE SEQUENCE BADGE_ID_SEQUENCE   
start with 1      
increment by 1    
nocache   
nocycle;   

但是,该过程会编译以下错误:

LINE/COL ERROR
-------- -----------------------------------------------------------------
11/5     PLS-00103: Encountered the symbol "end-of-file" when expecting
        one of the following:
        ( begin case declare end exception exit for goto if loop mod
        null pragma raise return select update while with
        <an identifier> <a double-quoted delimited-identifier>
        <a bind variable> << continue close current delete fetch lock
        insert open rollback savepoint set sql execute commit forall
        merge pipe purge

我做错了什么?

2 个答案:

答案 0 :(得分:3)

syntax for a loopLOOP ... END LOOP;,不含BEGIN

create procedure BADGE_PROCEDURE (badges NUMBER) AS  
  loops NUMBER;  
BEGIN  
  loops := badges;  
  loops := loops - 1;  
  LOOP  
    insert into BADGE values (BADGE_ID_SEQUENCE.nextval, 'prova', 'prova', DATE '2001-01-01');       
    loops := loops - 1;  
    EXIT WHEN loops < 0;  
  END LOOP;  
END;  
/

然而,更简单的版本是:

create procedure BADGE_PROCEDURE (badges NUMBER) AS  
BEGIN  
  FOR loops IN 1 .. badges LOOP  
    insert into BADGE values (BADGE_ID_SEQUENCE.nextval, 'prova', 'prova', DATE '2001-01-01');       
  END LOOP;  
END;  
/

答案 1 :(得分:2)

您的循环语法错误..

只做

             LOOP  
                     insert into BADGE values (BADGE_ID_SEQUENCE.nextval, 'prova', 'prova', '01-JAN-01');       
                     loops := loops - 1;  
                     EXIT WHEN loops < 0;  
             END LOOP;  

这是来自oracle docs的example