2012-02-29 98 views
-1

我一直在試圖運行此腳本,但我得到的是這樣的錯誤:出現符號「IS」期待之一,當以下

Error report: 
ORA-06550: line 3, column 15: 

PLS-00103: Encountered the symbol "IS" when expecting one of the following: 

constant exception <an identifier> 
<a double-quoted delimited-identifier> table long double ref 
char time timestamp interval date binary national character 
nchar 
The symbol "IS" was ignored. 

這裏是我的腳本:

set serveroutput on 

    DECLARE 
    cursor depts_cur is select dname from dept; 
    depts_cur_rec is depts_cur%type; 

    BEGIN 
    loop 
    fetch depts_cur into depts_cur_rec; 
    exit when depts_cur_rec%notfound; 
    dbms_output.put_line('Department: ' || depts_cur_rec); 
    end loop; 
    close depts_cur; 
    END; 

您的幫助將不勝感激。

+2

如果你讀了錯誤,它告訴你究竟是什麼錯誤:第3行是'depts_cur_rec是depts_cur%類型;';第15列是「IS」的地方。 – 2012-03-01 02:04:31

回答

6

它看起來像你的depts_cur_rec聲明是錯誤的,試試這個(去掉「是」):

set serveroutput on 

DECLARE 
    cursor depts_cur is select dname from dept; 
    depts_cur_rec depts_cur%type; 
BEGIN 
    BEGIN loop 
     fetch depts_cur 
     into depts_cur_rec; 
     exit when depts_cur_rec%notfound; 
     dbms_output.put_line('Department: ' || depts_cur_rec); 
    end loop; 
    close depts_cur; 
END; 
1

首先,當你聲明像depts_cur_rec一個局部變量,你不使用IS關鍵字。並且您希望depts_cur_rec被宣佈爲%ROWTYPE,而不是%TYPE。一旦你過去了,你的代碼就會失敗,因爲在提取之前你並沒有打開光標,因爲你正在嘗試使用記錄的%NOTFOUND屬性而不是光標。最後,您DBMS_OUTPUT調用需要從記錄

DECLARE 
    cursor depts_cur is select dname from dept; 
    depts_cur_rec depts_cur%rowtype; 
BEGIN 
    open depts_cur; 
    loop 
    fetch depts_cur into depts_cur_rec; 
    exit when depts_cur%notfound; 
    dbms_output.put_line('Department: ' || depts_cur_rec.dname); 
    end loop; 
    close depts_cur; 
END; 

很可能要容易得多,但是引用一個特定列,簡單地用隱式遊標而不顯遊標這樣的事情。這樣,就不需要手動指定OPEN,FETCHCLOSE。無需手動確定何時退出循環。沒有必要手動聲明記錄類型。你只需讓Oracle處理該

BEGIN 
    FOR dept_rec IN (SELECT dname 
        FROM dept) 
    LOOP 
    dbms_output.put_line('Department: ' || dept_rec.dname); 
    END LOOP; 
END; 

如果你想聲明光標外的線,你可以做到這一點與隱式遊標以及

DECLARE 
    CURSOR depts_cur 
     IS SELECT dname 
      FROM dept; 
BEGIN 
    FOR dept_rec IN depts_cur 
    LOOP 
    dbms_output.put_line('Department: ' || dept_rec.dname); 
    END LOOP; 
END; 
相關問題