plsql編程中游標的使用

 

遊標(Cursor):用來查詢數據庫,獲取記錄集合(結果集)的指針,能夠讓開發者一次訪問一行結果集,在每條結果集上做操做數據庫

 

oracle中顯示使用遊標通常要包含如下5個步驟:oracle

  • 聲明一些變量以便存儲從遊標返回的值。
  • 聲明遊標,並指定查詢。
  • 打開遊標。
  • 遍歷遊標並取得數據。
  • 關閉遊標

 

表結構及數據以下:oop

 1 -- Create table
 2 create table EXCHANGETIME
 3 (
 4   ID          NUMBER(18) default 0 not null,
 5   SYSTEM_TYPE CHAR(1) default ' ' not null,
 6   TIME_KIND   NUMBER(10) default 0 not null,
 7   TIME_NAME   VARCHAR2(16) default ' ' not null,
 8   BEGIN_TIME  NUMBER(10) default to_number(to_char(sysdate,'hh24miss')) not null,
 9   END_TIME    NUMBER(10) default to_number(to_char(sysdate,'hh24miss')) not null
10 )
11 
12 insert into EXCHANGETIME (ID, SYSTEM_TYPE, TIME_KIND, TIME_NAME, BEGIN_TIME, END_TIME)
13 values (1, '0', 0, '上午交易時間', 91500, 113000);
14 
15 insert into EXCHANGETIME (ID, SYSTEM_TYPE, TIME_KIND, TIME_NAME, BEGIN_TIME, END_TIME)
16 values (2, '0', 1, '下午交易時間', 130000, 150000);
17 
18 insert into EXCHANGETIME (ID, SYSTEM_TYPE, TIME_KIND, TIME_NAME, BEGIN_TIME, END_TIME)
19 values (3, '1', 2, '盤後交易時間', 150000, 153000);

 

 1 -- Create table
 2 create table BACKUPINFO
 3 (
 4   ID         NUMBER(18) default 0 not null,
 5   INIT_DATE  NUMBER(10) default to_number(to_char(sysdate,'yyyymmdd')) not null,
 6   TREAT_FLAG VARCHAR2(120) default ' ' not null,
 7   ERROR_INFO VARCHAR2(4000) default ' ' not null,
 8   FLAG       CHAR(1) default ' ' not null
 9 )
10 
11 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
12 values (1, 20140923, '1', '343%3r3', '2');
13 
14 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
15 values (2, 19900909, '4', 'fr454', ' ');
16 
17 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
18 values (1, 20140923, '1', '343%3r3', '2');
19 
20 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
21 values (2, 19900909, '4', 'fr454', ' ');
22 
23 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
24 values (1, 20140923, '1', '343%3r3', '2');
25 
26 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
27 values (2, 19900909, '4', 'fr454', ' ');
28 
29 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
30 values (1, 20140923, '1', '343%3r3', '2');
31 
32 insert into backupinfo (ID, INIT_DATE, TREAT_FLAG, ERROR_INFO, FLAG)
33 values (2, 19900909, '4', 'fr454', ' ');

 

下面來看幾個例子:fetch

1. 完整的示例:spa

  

 1 declare
 2   --聲明變量(用於存放遊標查詢出來的值)
 3   id exchangetime.id%type;
 4   system_type exchangetime.system_type%type;
 5   time_kind exchangetime.time_kind%type;
 6   time_name exchangetime.time_name%type;
 7   begin_time exchangetime.begin_time%type;
 8   end_time exchangetime.end_time%type;
 9   
10   --聲明遊標
11   cursor v_eq is select * from exchangetime;
12 begin
13   --打開遊標
14   open v_eq;
15   
16   loop
17     --從遊標中取出每行數據賦給上面定義的變量並打印出來
18     fetch v_eq into id, system_type, time_kind, time_name, begin_time, end_time;
19     dbms_output.put_line(id || ' ' || system_type|| 'time_kind:' || time_kind || ' time _name:' || time_name || ' ' || begin_time || '-->' || end_time);
20     
21     exit when v_eq%NOTFOUND;
22   end loop;
23   
24   --關閉遊標
25   close v_eq;
26   
27 end;
28 /

輸出結果:
1 0time_kind:0 time _name:上午交易時間 91500-->113000
2 0time_kind:1 time _name:下午交易時間 130000-->150000
3 1time_kind:2 time _name:盤後交易時間 150000-->153000
3 1time_kind:2 time _name:盤後交易時間 150000-->153000指針

 

2. 與for循環聯合使用(推薦用法)   code

1 declare
2 begin
3   --mcursor的名字能夠隨便定義,aa或者bb都行,這樣寫的好處是不用顯示定義遊標,打開遊標循環取值再關閉遊標,很方便。
4  for mcursor in (select * from exchangetime) loop
5    dbms_output.put_line(mcursor.id || mcursor.time_name);
6  end loop;
7   
8 end;
9 /

結果以下:blog

  1上午交易時間
  2下午交易時間
  3盤後交易時間開發


3.使用open ... for ...語句get

 1 declare  2 --定義遊標指針並指定返回類型爲exchangetime的全部列  3 type t_cursor is ref cursor return exchangetime%rowtype;  4 --定義接收返回值的變量  5 v_etresult exchangetime%rowtype;  6 --至關於定義了一個指針變量  7  v_cursor t_cursor;  8 begin  9 --將指針並指向一個遊標並打開 10 open v_cursor for select * from exchangetime t where t.id<3; 11 12  loop 13 -- --指向的遊標裏面每一行的值賦給接收的變量,並打印出來 14 fetch v_cursor into v_etresult; 15  dbms_output.put_line(v_etresult.time_name); 16 exit when v_cursor%notfound; 17 end loop; 18 --關閉指向的遊標 19 close v_cursor; 20 21 --將指針指向另一個遊標並打開 22 open v_cursor for select * from exchangetime t; 23 24  loop 25 fetch v_cursor into v_etresult; 26 dbms_output.put_line(v_etresult.time_name|| '--' || v_etresult.system_type); 27 exit when v_cursor%notfound; 28 end loop; 29 30 31 --關閉指針指向的遊標 32 close v_cursor; 33 end; 34 /

 結果以下:

上午交易時間
下午交易時間
下午交易時間
上午交易時間--0
下午交易時間--0
盤後交易時間--1
盤後交易時間--1

4.無約束遊標:前面的遊標都有返回類型稱爲約束遊標,約束遊標的返回類型必須與遊標運行時查詢中的列相匹配。無約束遊標沒有返回類型所以能夠運行任何查詢。

 

 1 declare
 2   --定義遊標指針
 3   type t_cursor is ref cursor;
 4   --定義接收返回值的變量
 5   v_etresult exchangetime%rowtype;
 6   v_buresult backupinfo%rowtype;
 7   --至關於定義了一個指針變量
 8   v_cursor t_cursor;
 9 begin
10   --將指針並指向一個遊標並打開
11   open v_cursor for select * from exchangetime t where t.id<3;
12   
13   loop
14     --指向的遊標裏面每一行的值賦給接收的變量,並打印出來
15     fetch v_cursor into v_etresult;
16     dbms_output.put_line(v_etresult.time_name);
17     exit when v_cursor%notfound;
18   end loop;
19   --關閉指向的遊標
20   close v_cursor;
21   
22   --將指針指向另一個遊標並打開
23    open v_cursor for select * from backupinfo t;
24    
25    loop
26      --指向的遊標裏面每一行的值賦給接收的變量,並打印出來
27     fetch v_cursor into v_buresult;
28     dbms_output.put_line(v_buresult.flag|| '--' || v_buresult.init_date);
29     exit when v_cursor%notfound;
30    end loop;
31   
32   
33   --關閉指針指向的遊標
34   close v_cursor;
35 end;
36 /

結果以下:    上午交易時間    下午交易時間    下午交易時間    2--20140923     --19900909    2--20140923      --19900909    2--20140923      --19900909    2--20140923      --19900909      --19900909

相關文章
相關標籤/搜索