2015-05-08 40 views
-1

我試圖運行此代碼,但它給了我一個「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;` 

回答

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,編譯錯誤應該消失。

+0

謝謝你,我想通了:) –

相關問題