2014-01-18 65 views
0

我想寫一個簡單的光標並打開,獲取值並關閉。以下是我的光標代碼。錯誤,而在plsql中運行簡單的光標

DECLARE 
    id cur.sid%TYPE; 
    s_num cur.serial#%TYPE; 
    CURSOR cur IS 
    SELECT sid, serial# 
    FROM v$session WHERE username = 'ABC'; 
BEGIN 
    OPEN cur; 

    LOOP 
    FETCH c cur INTO id, s_num; 
    dbms_output.put_line(id || ' ' || s_num); 
    END LOOP; 

    CLOSE cur; 
END; 
/

當運行此我看到以下錯誤消息:

FETCH c cur INTO id, s_num; 
     * 
ERROR at line 9: 
ORA-06550: line 9, column 9: 
PLS-00103: Encountered the symbol "CUR" when expecting one of the following: 
. into bulk 
The symbol "." was substituted for "CUR" to continue. 

任何種類的方向來解決這個之一被理解。謝謝。

+1

抓取只需要鼠標的名字 - 「C」 是沒有必要的。嘗試FETCH cur INTO id,scum; – racraman

回答

0
  • FETCH c cur INTO id, s_num;c是多餘的,刪除它。
  • 更改ID和s_num的認定中作爲followes:
    id v$session.sid%type;
    s_num v$session.serial#%type;

  • exit when cur%notfound;作爲循環的退出條件。

    DECLARE 
    CURSOR cur IS 
    SELECT sid, serial# FROM v$session where username = 'ABC'; 
    id v$session.sid%type; 
    s_num v$session.serial#%type; 
    BEGIN 
    OPEN cur; 
    LOOP 
    FETCH cur 
        INTO id, s_num; 
        exit when cur%notfound; 
        dbms_output.put_line(id || ' ' || s_num); 
    END LOOP; 
    CLOSE cur; 
    END; 
    
1

使用IMPLICIT開放和光標的獲取的簡化版本(參數化)

DECLARE 
    CURSOR cur(v_USER VARCHAR2) 
    IS 
    SELECT sid, serial# FROM v$session where username = v_USER; 
BEGIN 
    FOR I IN cur('ABC') 
    LOOP 
    dbms_output.put_line(I.sid || ' ' || I.serial#); 
    END LOOP; 
END;