嗨 甲骨文【2】

來源:互聯網
上載者:User

/************************************************
我與遊標

/************************************************
使用顯式遊標

定義遊標DECLARE
CURSOR cursor_name IS select_statement;

開啟遊標OPEN
OPEN cursor_name

提取遊標FETCH
FETCH cursor_name INTO variable1,variable2,...;

關閉遊標CLOSE
CLOSE cursor_name;

顯式遊標屬性
%ISOPEN判斷是否已經開啟 開啟TRUE
%FOUND是否從結果集中提取到了資料 提取到TRUE
%NOTFOUND是否從結果集中提取到了資料 沒有提取到TRUE
%ROWCOUNT返回到當前行為止已經提取到的實際行數

參數遊標

以實現使用不同參數值多次開啟遊標時,可以產生不同的結果集
CURSOR cursor_name(parameter_name datatype) IS select_statement;

使用遊標更新/刪除資料
CURSOR cursor_name(parameter_name datatype) IS select_statement FOR UPDATE [OF column_reference] [NOWAIT];

FOR UPDATE用於在結果集資料上加行共用鎖定,以防止其他使用者再次行執行DML操作
OF確定那些表要加鎖
NOWAIT指定執行時不等待鎖,如果其他會話已經在被作用行上加鎖,則當前會話顯示錯誤提示

UPDATE table_name SET column=.. WHERE CURRENT OF cursor_name;
DELETE table_name WHERE CURRENT OF cursor_name;

遊標迴圈

Oracle會隱含地開啟遊標,提取遊標並關閉遊標
FOR record_name IN cursor_name LOOP
    statement1;
    statement2;
    ...
END LOOP;

如果迴圈時不需要使用任何遊標屬性,則可直接使用子查詢
FOR record_name IN (select_statement) LOOP
    statement1;
    statement2;
    ...
END LOOP;

使用遊標變數

定義REF CURSOR類型和遊標變數
TYPE ref_type_name IS REF CURSOR [RETURN return_type];
cursor_variable ref_type_name;

開啟遊標
OPEN cursor_variable FOR select_statement;

提取遊標資料
FETCH cursor_variable INTO variable1,variable2,...;

關閉遊標CLOSE
CLOSE cursor_variable


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.