首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫

Oracle 10g R2 Ref Cursor 新功能:將游標作為輸入參數傳遞給 PL/SQL 存儲過程

     編輯:Oracle 資料庫相關知識

Ref Cursor 回顧

Ref Cursor 為伺服器端結果集參考。開啟時不回傳資料,僅回傳伺服器端地址,客戶端自主決定提取時機與方式。舊版 ODP.NET 僅支援輸出參數接收 Ref Cursor,無法作為輸入參數傳遞。

10g R2 突破:Ref Cursor 作為 IN 參數

可將客戶端持有的 Ref Cursor 直接傳入 PL/SQL 程序處理,極大擴展應用彈性。

完整實戰範例

1. 資料庫端準備

CREATE TABLE processing_result (status VARCHAR2(64));

CREATE OR REPLACE PACKAGE cursor_in_out AS
  TYPE emp_cur_type IS REF CURSOR RETURN employees%ROWTYPE;
  PROCEDURE process_cursor(p_cursor IN emp_cur_type);
END;
/

CREATE OR REPLACE PACKAGE BODY cursor_in_out AS
  PROCEDURE process_cursor(p_cursor IN emp_cur_type) IS
    emp_rec employees%ROWTYPE;
  BEGIN
    LOOP
      FETCH p_cursor INTO emp_rec;
      EXIT WHEN p_cursor%NOTFOUND;
      INSERT INTO processing_result VALUES(
        'Processed employee #' || emp_rec.employee_id || ': ' ||
        emp_rec.first_name || ' ' || emp_rec.last_name
      );
    END LOOP;
  END;
END;
/

2. .NET 端程式碼

using System; using System.Data; using Oracle.DataAccess.Client; using Oracle.DataAccess.Types;

string connStr = "User Id=hr;Password=hr;Data Source=oramag;Pooling=false";
using (OracleConnection con = new OracleConnection(connStr)) {
  con.Open();
  
  // 步驟 1:取得 Ref Cursor (OUTPUT)
  OracleCommand cmd = con.CreateCommand();
  cmd.CommandText = "BEGIN OPEN :1 FOR SELECT * FROM employees WHERE manager_id=101; END;";
  OracleParameter p_rc = cmd.Parameters.Add("p_rc", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output);
  cmd.ExecuteNonQuery();
  
  // 步驟 2:將 Ref Cursor 作為 INPUT 傳入存儲過程
  cmd.Parameters.Clear();
  cmd.CommandText = "cursor_in_out.process_cursor";
  cmd.CommandType = CommandType.StoredProcedure;
  OracleParameter p_input = cmd.Parameters.Add("p_input", OracleDbType.RefCursor, p_rc.Value, ParameterDirection.Input);
  cmd.ExecuteNonQuery();
  
  // 清理
  p_input.Dispose(); p_rc.Dispose(); cmd.Dispose();
}

3. 驗證結果

SQL> SELECT * FROM processing_result;
STATUS
----------------------------------------
Processed employee #108: Nancy Greenberg
Processed employee #200: Jennifer Whalen
Processed employee #203: Susan Mavris
Processed employee #204: Hermann Baer
Processed employee #205: Shelley Higgins

應用價值

  • 客戶端可先行篩選/轉換資料,再交由資料庫端統一處理邏輯
  • 減少網路往返,提升批次處理效能
  • 支援跨資料庫異構資料整合場景

版本需求

  • Oracle Database 10g Release 2 (10.2.0.1+)
  • ODP.NET 10.2.0.2.20+
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved