-1

我正在尝试运行此代码,但它在“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;`
4

1 回答 1

1

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

出现错误是因为 Oracle 认为(字符在 之后出现max,但它却看到THEN了。我看到的错误全文是

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

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

在 PL/SQL 中,通常最好在局部变量前面加上l_or v_。除了避免 Oracle 内置函数(如MAX)外,它还可以帮助您避免与恰好与局部变量相同的列名发生名称冲突。

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

于 2015-05-08T18:47:25.103 回答