PLS-00103:在遇到以下其中一项时遇到符号“THEN”

问题描述:

我试图运行此代码,但它给了我一个“THEN”中的错误,我已经逐行检查了所有代码,错误是在if语句中,但我仔细检查了它。PLS-00103:在遇到以下其中一项时遇到符号“THEN”

我想比较事故发生的时间,所以我可以将救护车送到首次发生的事故。我会感激你的帮助

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

的问题是,你有一个名为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,编译错误应该消失。

+0

谢谢你,我想通了:) –