成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Oracle數據庫游標的類型及使用實例全解

數據庫 Oracle
本文我們主要介紹了Oracle數據庫的五種類型的游標的使用,這五種游標分別是:隱式游標、顯式游標、REF CURSOR、BULK SQL和動態性能表V$OPEN_CURSOR,希望能夠對您有所幫助。

游標是SQL的一個內存工作區,由系統或用戶以變量的形式定義。游標的作用就是用于臨時存儲從數據庫中提取的數據塊。Oracle數據庫的Cursor類型包含三種: 靜態游標:分為顯式(explicit)游標和隱式(implicit)游標;REF游標:是一種引用類型,類似于指針。下面我們一一介紹它們的使用。

1.隱式游標

1)Select …INTO…語句,DML語句,使用隱式Cursor。此外,還有一種使用FOR LOOP的Implicit Cursor用法。

2)可以通過隱式Cusor的屬性來了解操作的狀態和結果。Cursor的屬性包含:

SQL%ROWCOUNT 整型代表DML語句成功執行的數據行數。

SQL%FOUND  布爾型值為TRUE代表插入、刪除、更新或單行查詢操作成功。

SQL%NOTFOUND 布爾型與SQL%FOUND屬性返回值相反。

SQL%ISOPEN 布爾型DML執行過程中為真,結束后為假。

3) 隱式Cursor由系統自動打開和關閉.

例如:

  1. set serveroutput on    
  2.  
  3. declare    
  4.  
  5. begin      
  6.  
  7. update employees set employee_name='Mike' where employee_id=1001;    
  8.  
  9. if SQL%FOUND then      
  10.  
  11. dbms_output.put_line('Name is updated');    
  12.  
  13. else    
  14.  
  15. dbms_output.put_line('Name is not updated');    
  16.  
  17. end if;    
  18.  
  19. end;    
  20.  
  21. /    
  22.  
  23. set serveroutput on    
  24.  
  25. declare    
  26.  
  27. begin      
  28.  
  29. for tableInfo in (select * from user_tables) loop    
  30.  
  31. dbms_output.put_line(tableInfo.table_name);    
  32.  
  33. end loop;    
  34.  
  35. exception    
  36.  
  37. when others then    
  38.  
  39. dbms_output.put_line(sqlerrm);    
  40.  
  41. end;    
  42.  
  43. /  

2.顯式游標

1) 顯式Cursor的屬性包含:

游標的屬性   返回值類型   意義 

%ROWCOUNT   整型  獲得FETCH語句返回的數據行數 

%FOUND  布爾型 最近的FETCH語句返回一行數據則為真,否則為假 

%NOTFOUND   布爾型 與%FOUND屬性返回值相反 

%ISOPEN 布爾型 游標已經打開時值為真,否則為假  

2) 對于顯式游標的運用分為四個步驟:

a 定義游標---Cursor  [Cursor Name]  IS;

b 打開游標---Open  [Cursor Name]; 

c  操作數據---Fetch  [Cursor name] 

d  關閉游標---Close [Cursor Name]

以下是幾種常見顯式Cursor用法。

  1. <p>set serveroutput on    
  2.  
  3. declare    
  4.  
  5. cursor cur is select * from user_tables;    
  6.  
  7. tableInfo user_tables%rowtype;    
  8.  
  9. begin    
  10.  
  11. open cur;        
  12.  
  13. loop    
  14.  
  15. fetch cur into tableInfo;    
  16.  
  17. exit when cur%notfound;    
  18.  
  19. dbms_output.put_line(tableInfo.table_name);    
  20.  
  21. end loop;</p><p>exception    
  22.  
  23. when others then    
  24.  
  25. dbms_output.put_line(sqlerrm);</p><p>  close cur;    
  26.  
  27. end;    
  28.  
  29. /</p>    
  30.  
  31. set serveroutput on    
  32.  
  33. declare    
  34.  
  35. cursor cur is select * from user_tables;    
  36.  
  37. begin      
  38.  
  39. for tableInfo in cur loop    
  40.  
  41. dbms_output.put_line(tableInfo.table_name);    
  42.  
  43. end loop;    
  44.  
  45. exception    
  46.  
  47. when others then    
  48.  
  49. dbms_output.put_line(sqlerrm);    
  50.  
  51. end;    
  52.  
  53. /  

還可以使用帶參數open的cursor。

  1. <p>set serveroutput on    
  2.  
  3. declare    
  4.  
  5. cursor cur(tblName varchar2) is select * from user_constraints where table_name=tblName;    
  6.  
  7. tableInfo user_constraints%rowtype;    
  8.  
  9. begin    
  10.  
  11. open cur('EMPLOYEES');        
  12.  
  13. loop    
  14.  
  15. fetch cur into tableInfo;    
  16.  
  17. exit when cur%notfound;    
  18.  
  19. dbms_output.put_line(tableInfo.constraint_name);    
  20.  
  21. end loop;</p><p>exception    
  22.  
  23. when others then    
  24.  
  25. dbms_output.put_line(sqlerrm);</p><p>  close cur;    
  26.  
  27. end;    
  28.  
  29. /</p><p></p>    
  30.  
  31. set serveroutput on    
  32.  
  33. declare    
  34.  
  35. cursor cur(tblName varchar2) is select * from user_constraints where table_name=tblName;    
  36.  
  37. begin    
  38.  
  39. for tableInfo in cur('EMPLOYEES') loop    
  40.  
  41. dbms_output.put_line(tableInfo.constraint_name);    
  42.  
  43. end loop;    
  44.  
  45. exception    
  46.  
  47. when others then    
  48.  
  49. dbms_output.put_line(sqlerrm);    
  50.  
  51. end    
  52.  
  53. /  

可以使用WHERE CURRENT OF子句執行UPDATE或DELETE操作。

  1. set serveroutput on    
  2.  
  3. declare    
  4.  
  5. cursor cur is select * from employees for update;    
  6.  
  7. begin      
  8.  
  9. for tableInfo in cur loop    
  10.  
  11. update employees set salarysalary=salary*1.1 where current of cur;    
  12.  
  13. end loop;    
  14.  
  15. commit;    
  16.  
  17. exception    
  18.  
  19. when others then    
  20.  
  21. dbms_output.put_line(sqlerrm);    
  22.  
  23. end;    
  24.  
  25. /  

3.REF CURSOR(Cursor Variables)

REF Cursor在運行的時候才能確定游標使用的查詢。利用REF CURSOR,可以在程序間傳遞結果集(一個程序里打開游標變量,在另外的程序里處理數據)。

也可以利用REF CURSOR實現BULK SQL,提高SQL性能。

REF CURSOR分兩種,Strong REF CURSOR 和 Weak REF CURSOR。

Strong REF CURSOR:指定retrun type,CURSOR變量的類型必須和return type一致。

Weak REF CURSOR:不指定return type,能和任何類型的CURSOR變量匹配。

Ref cursor的使用:

1) Type [Cursor type name] is ref cursor 

2) Open cursor for...

3) Fetch  [Cursor name] 

4) Close Cursor

例如:

Step1:

  1. create or replace package TEST as    
  2.  
  3. type employees_refcursor_type is ref cursor return employees%rowtype;    
  4.  
  5. procedure employees_loop(employees_cur IN employees_refcursor_type);    
  6.  
  7. end TEST;    
  8.  
  9. /   

Step2:

  1. create or replace package body TEST as    
  2.  
  3. procedure employees_loop(employees_cur IN employees_refcursor_type) is    
  4.  
  5. emp employees%rowtype;    
  6.  
  7. begin    
  8.  
  9. loop    
  10.  
  11. fetch employees_cur into emp;    
  12.  
  13. exit when employees_cur%NOTFOUND;    
  14.  
  15. dbms_output.put_line(emp.employee_id);    
  16.  
  17. end loop;    
  18.  
  19. end employees_loop;    
  20.  
  21. end TEST;    
  22.  

Step3:

  1. set serveroutput on    
  2.  
  3. declare    
  4.  
  5. empRefCur TEST.employees_refcursor_type;    
  6.  
  7. begin    
  8.  
  9. for i in 10..20 loop    
  10.  
  11. dbms_output.put_line('Department ID=' || i);    
  12.  
  13. open empRefCur for select * from employees where department_id=i;    
  14.  
  15. TEST.employees_loop(empRefCur);    
  16.  
  17. end loop;    
  18.  
  19. exception    
  20.  
  21. when others then    
  22.  
  23. dbms_output.put_line(sqlerrm);    
  24.  
  25.  
  26. close empRefCur;    
  27.  
  28. end;    
  29.  

4.BULK SQL

使用FORALL和BULK COLLECT子句。利用BULK SQL可以減少PLSQL Engine和SQL Engine之間的通信開銷,提高性能。

1. To speed up INSERT, UPDATE, and DELETE statements, enclose the SQL statement within a PL/SQL FORALL statement instead of a loop construct. 加速INSERT, UPDATE, DELETE語句的執行,也就是用FORALL語句來替代循環語句。

2. To speed up SELECT statements, include the BULK COLLECT INTO clause in the SELECT statement instead of using INTO.  加速SELECT,用BULK COLLECT INTO 來替代INTO。

  1. SQL> create table employees_tmp as select first_name, last_name, salary from employees where 0=1;  
  2.  
  3. set serveroutput on    
  4.  
  5. declare    
  6.  
  7. cursor employees_cur(depId employees.department_id%type) is select first_name, last_name, salary from employees where department_id=depId;    
  8.  
  9. type employee_table_type is table of employees_cur%rowtype index by pls_integer;    
  10.  
  11. employee_table employee_table_type;    
  12.  
  13. begin    
  14.  
  15. open employees_cur(100);    
  16.  
  17. fetch employees_cur bulk collect into employee_table;    
  18.  
  19. close employees_cur;    
  20.  
  21. for i in 1..employee_table.count loop    
  22.  
  23. dbms_output.put_line(employee_table(i).first_name || ' ' || employee_table(i).last_name || ',' || employee_table(i).salary);    
  24.  
  25. end loop;    
  26.  
  27. forall i in employee_table.first..employee_table.last    
  28.  
  29. insert into employees_tmp values(employee_table(i).first_name, employee_table(i).last_name, employee_table(i).salary);    
  30.  
  31. commit;    
  32.  
  33. end;    
  34.  
  35. /   

5.  動態性能表V$OPEN_CURSOR

本視圖列出session打開的所有cursors。

關于Oracle數據庫游標的類型和使用的知識就介紹到這里了,如果您想了解更多的Oracle數據庫的知識,可以到這里看一下:http://database.51cto.com/oracle/,相信一定能夠帶給您收獲的!

【編輯推薦】

  1. MySQL數據庫如何實現跨表更新與數據并合
  2. 關于Oracle利用UTL_INADDR注入的簡單介紹
  3. MySQL數據庫修改MySQL密碼的六種措施總結
  4. Oracle數據庫定時器Job在各個時間的寫法總結篇
  5. 關于MySQL數據庫索引和ORDER BY子句的使用問題簡介
責任編輯:趙鵬 來源: CSDN博客
相關推薦

2010-04-21 15:02:50

Oracle使用游標

2010-09-01 15:15:20

DB2動態游標

2011-05-19 13:25:14

Oracle數據庫

2010-10-09 16:41:54

MYSQL存儲過程

2010-04-21 15:10:35

Oracle游標

2011-08-11 16:55:34

Oracle數據庫AWR

2011-07-21 16:28:20

MySQL數據庫帶游標的存儲過程

2011-03-29 10:47:49

ORACLE數據庫

2010-10-28 16:46:23

查詢Oracle數據庫

2010-05-06 11:02:26

Oracle游標

2010-05-07 12:07:08

Oracle 多層游標

2010-09-08 09:11:42

SQL游標語法

2022-05-11 15:06:02

MySQL游標SQL

2010-05-26 16:41:09

MySQL 游標

2010-04-06 11:30:09

Oracle 數據庫

2010-04-13 10:55:35

Oracle數據庫

2010-04-14 15:45:49

Oracle 數據庫

2010-04-23 09:32:39

Oracle數據庫實例

2011-08-22 14:00:13

SQL Server 游標調用函數

2011-08-18 10:21:50

SQL ServerDATEPART
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 视频一区二区中文字幕 | 国产精品一区二区在线 | 国产精品美女久久久久久免费 | 欧美一区二区免费视频 | 91国内精品久久 | 91九色视频| 欧美一区二区三区在线 | 亚洲国产精品一区二区第一页 | 91亚洲一区 | 精品久久久久一区二区国产 | 老头搡老女人毛片视频在线看 | 国产高清毛片 | 国产精品高清一区二区 | 一级做受毛片免费大片 | 亚洲精品免费视频 | 男女羞羞视频在线观看 | 操视频网站 | 国产一区二区三区 | 欧美极品在线观看 | 99色综合 | 老司机精品福利视频 | 欧美日韩国产一区二区三区不卡 | 毛片毛片毛片毛片 | 三级欧美 | 久久久久久国产精品久久 | 亚洲欧洲一区二区 | 精品一区二区久久久久久久网站 | 人人人艹| 九久久 | 日韩av在线一区二区 | 国产欧美日韩视频 | 国产探花在线精品一区二区 | 久久久.com | 欧美一级久久 | 羞羞视频在线观看 | 免费亚洲一区二区 | 酒色成人网 | 欧美日韩国产在线观看 | 久久噜噜噜精品国产亚洲综合 | 亚洲国产成人精品久久久国产成人一区 | 亚洲欧美中文字幕在线观看 |