PLS-00103:遇到了符号"那么"当期待以下之一

时间:2015-05-08 17:30:12

标签: oracle plsql pls-00103

我正在尝试运行此代码,但它在" THEN"中给了我一个错误,我已经逐行检查了所有代码,看起来错误在if语句中,但是我双重检查它

我想比较事故发生的时间,这样我就能把救护车送到事故发生的地方。我将非常感谢您的帮助

`create or replace function get_loc return location is
max NUMBER;
CURSOR accident_records IS
SELECT * FROM NEW_ACCIDENT;
accidentRec NEW_ACCIDENT_TYPE := NEW_ACCIDENT_TYPE (NULL,NULL,NULL,NULL);
ac_loc LOCATION := LOCATION (NULL,NULL);
type New_accident_rec_type is record
(
id number,
loc location,
TIME NUMBER,
SITUATION varchar2(60)
);
new_accident_rec New_accident_rec_type;
BEGIN
max:=0;
OPEN accident_records;
LOOP FETCH accident_records INTO new_accident_rec;
EXIT WHEN accident_records%NOTFOUND;
IF new_accident_rec.situation='not handled' then
IF new_accident_rec.time>max THEN
max:=new_accident_rec.time;
accidentRec.time:=new_accident_rec.time;
ac_loc:=new_accident_rec.loc;
END IF;
IF new_accident_rec.time<max THEN
ac_loc:=NULL;
END IF;
END IF;
END LOOP;
CLOSE accident_records;
dbms_output.put_line ('The time of Accident is: '||accidentRec.time || 'The location of the accident is: ' ||ac_loc);
RETURN ac_loc;
END;`

1 个答案:

答案 0 :(得分:1)

问题是你有一个名为max的局部变量,它与Oracle MAX聚合函数冲突。

出现错误是因为Oracle认为(后面有max字符,但它会看到THEN。我看到的错误的全文是

LINE/COL ERROR
-------- -----------------------------------------------------------------
22/42    PLS-00103: Encountered the symbol "THEN" when expecting one of
         the following:
         (

(我可能在运行之前重新格式化了代码;如果行/列号不匹配,请不要担心。)

在PL / SQL中,通常使用l_v_为局部变量加前缀。除了避免像MAX这样的Oracle内置函数之外,它还可以帮助您避免名称冲突,其列名恰好与您的本地变量相同。

希望如果您将max变量重命名为l_max,则编译错误应该消失。