错误(34,24):PLS-00103:预期以下情况之一时遇到符号“文件结束”

时间:2019-12-17 09:12:15

标签: oracle plsql

我正在编写oracle函数,并且遇到一些编译时错误;

create or replace FUNCTION GETSUBLOCATIONS 
(
P_LocationID VARCHAR2
)RETURN VARCHAR2 AS 
BEGIN
    Declare P_ID varchar2(500):='';
    begin
      INSERT INTO "HIL"."P_TEMP" (DATA)( SELECT PLANT_CODE 
       FROM "HIL"."TBLPLANTMASTER" 
       WHERE    PLANT_MASTER_LOCATION ='P_LocationID');
       DECLARE P_ROWS INT:=0;
        BEGIN
          SELECT COUNT(*)INTO P_ROWS FROM P_TEMP;


    IF(P_ROWS > 0)
    THEN
    DECLARE P_I INT  := 1;  
    BEGIN
    DECLARE P_STR VARCHAR2:='';
    BEGIN
    WHILE(P_I < P_ROWS)
    LOOP
    SELECT "HIL"."P_TEMP".data into P_STR FROM P_TEMP  WHERE ID = P_I;
    P_ID := P_ID + P_STR;
    PI:= PI + 1;
    END LOOP;        
    END;
  END;
                     END IF;
 END;
RETURN P_ID;

END GETSUBLOCATIONS;

我遇到以下错误

  

错误(34,24):PLS-00103:遇到符号“文件结尾”   预期以下情况之一:(开始情况声明结束异常   如果循环模式为null,则退出goto,返回编译更新   在
   <<继续关闭当前删除删除锁
  插入打开回滚保存点集sql执行落实所有合并   管道吹扫

2 个答案:

答案 0 :(得分:2)

如果您已经正确缩进了代码,问题将变得显而易见:

create or replace FUNCTION GETSUBLOCATIONS 
( P_LocationID VARCHAR2 ) RETURN VARCHAR2 AS 
BEGIN
    Declare P_ID varchar2(500):='';
    begin
        INSERT INTO "HIL"."P_TEMP" (DATA)( SELECT PLANT_CODE 
        FROM "HIL"."TBLPLANTMASTER" 
        WHERE    PLANT_MASTER_LOCATION ='P_LocationID');

        DECLARE P_ROWS INT:=0;
        BEGIN
            SELECT COUNT(*)INTO P_ROWS FROM P_TEMP;
            IF(P_ROWS > 0) THEN
                BEGIN
                    DECLARE P_I INT  := 1;  
                    DECLARE P_STR VARCHAR2:='';
                    BEGIN
                        WHILE(P_I < P_ROWS) LOOP
                            SELECT "HIL"."P_TEMP".data into P_STR FROM P_TEMP  WHERE ID = P_I;
                            P_ID := P_ID + P_STR;
                            PI:= PI + 1;
                        END LOOP;        
                    END;
                END;
            END IF;
        END;
        RETURN P_ID;
    ***MISSING END HERE***
END GETSUBLOCATIONS;

您错过了第一个END中的最后一个BEGIN

答案 1 :(得分:1)

BEGIN太多,无法在代码中声明行。请将您的代码减少为-

CREATE OR REPLACE FUNCTION GETSUBLOCATIONS(
    P_LocationID VARCHAR2)
  RETURN VARCHAR2
AS
P_ID VARCHAR2(500):='';
P_ROWS INT:=0;
P_I INT := 1;

BEGIN
    INSERT
    INTO "HIL"."P_TEMP"(DATA)
    SELECT PLANT_CODE
    FROM "HIL"."TBLPLANTMASTER"
    WHERE PLANT_MASTER_LOCATION =P_LocationID;

    SELECT COUNT(*)INTO P_ROWS FROM P_TEMP;
    IF(P_ROWS > 0) THEN
          FOR I IN (SELECT "HIL"."P_TEMP".data 
                    FROM P_TEMP
                    WHERE ID = P_I)       -- You must ommit where clause if you need all the IDs.
          LOOP
              P_ID := P_ID || I.data;
          END LOOP;
    END IF;
    RETURN P_ID;
EXCEPTION
         WHEN OTHERS THEN
              NULL;
END GETSUBLOCATIONS;
/