cursor 的簡單寫法和官方文檔對比

在存儲中使用遊標是件很常見的事情,傳統寫法比較麻煩。今天將化繁爲簡的寫法總結一下:

格式如下 

create or replace procedure ff
as 
   v_column_name
begin
     for v_info in (
                select column_name from cols where table_name = 'EMP'
                ) loop
                --定義和循環遊標
                dbms_output.put_line(v_info.column_name);
                --調用遊標中的值 
                end loop; 
end;
/*
  優點:
   1、不用指定接收遊標值的變量
  2、不用顯示的打開和關閉遊標
  缺點:
  1、無法使用遊標屬性notfound,isopen,found,notfound 
  2、無法使用動態sql 有使用動態sql需求的話 得用傳統遊標方式
  
*/

以下摘錄幾個官方文檔例子:

DECLARE
  CURSOR c1 (job VARCHAR2, max_wage NUMBER) IS
    SELECT * FROM employees
    WHERE job_id = job
    AND salary > max_wage;
BEGIN
  FOR person IN c1('ST_CLERK', 3000)
  LOOP
     -- process data record
    DBMS_OUTPUT.PUT_LINE (
      'Name = ' || person.last_name || ', salary = ' ||
      person.salary || ', Job Id = ' || person.job_id
    );
  END LOOP;
END;
/

Result:

Name = Nayer, salary = 4065.6, Job Id = ST_CLERK
Name = Mikkilineni, salary = 3430.35, Job Id = ST_CLERK
Name = Landry, salary = 3049.2, Job Id = ST_CLERK
...
Name = Vargas, salary = 3176.25, Job Id = ST_CLERK
例子2:

BEGIN
  FOR item IN (
    SELECT first_name || ' ' || last_name AS full_name,
           salary * 10                    AS dream_salary 
    FROM employees
    WHERE ROWNUM <= 5
    ORDER BY dream_salary DESC, last_name ASC
  ) LOOP
    DBMS_OUTPUT.PUT_LINE
      (item.full_name || ' dreams of making ' || item.dream_salary);
  END LOOP;
END;
/

Result:

Michael Hartstein dreams of making 143325
Pat Fay dreams of making 66150
Jennifer Whalen dreams of making 48510
Douglas Grant dreams of making 31531.5
Donald OConnell dreams of making 31531.5

注意:

Note:

When an exception is raised inside a cursorFOR LOOP statement, the cursor closes before the exception handler runs. Therefore, the values of explicit cursor attributes are not available in the handler.

當一個異常發生在for loop語句塊內部是,遊標關閉在異常被處理前。因此,在語句塊中顯示遊標的屬性無效

傳統方式:(摘錄自官方文檔)

You can either declare an explicit cursor first and then define it later in the same block, subprogram, or package, or declare and define it at the same time.

An explicit cursor declaration, which only declares a cursor, has this syntax:

CURSOR cursor_name [ parameter_list ] RETURN return_type;

An explicit cursor definition has this syntax:

CURSOR cursor_name [ parameter_list ] [ RETURN return_type ]
  IS select_statement;

If you declared the cursor earlier, then the explicit cursor definition defines it; otherwise, it both declares and defines it.

例子如下:

DECLARE
  CURSOR c1 RETURN departments%ROWTYPE;    -- 聲明 c1
 
  CURSOR c1 RETURN departments%ROWTYPE IS  -- 定義 c1,
    SELECT * FROM departments              
    WHERE department_id = 110;
  /*
    在實際使用中主要採用c2的方式聲明和定義遊標
    c1 和 c3的方式至今沒用到過。官方文檔 就是標準....
  */
  CURSOR c2 IS                             -- 聲明 and 定義 c2 
    SELECT employee_id, job_id, salary FROM employees
    WHERE salary > 2000; 
  /*
    以下方式也是常用寫法。
    遊標定義方式 OPEN C2 FOR 'SQL statements '
    好處在用可靈活使用綁定變量
  */
  TYPE cursor_type IS REF CURSOR;
  C2 cursor_type;
  
  CURSOR c3 RETURN locations%ROWTYPE;      -- 聲明 c3 
 
  CURSOR c3 IS                             -- 定義 c3,
    SELECT * FROM locations                -- 省略 return type
    WHERE country_id = 'JP';
BEGIN
  NULL;
END;
/





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章