一 概念
游标是SQL的一个内存工作区,由系统或用户以变量的形式定义。游标的作用就是用于临时存储从数据库中提取的数据块。在某些情况下,需要把数据从存放在磁盘的表中调到计算机内存中进行处理,最后将处理结果显示出来或最终写回数据库。这样数据处理的速度才会提高,否则频繁的磁盘数据交换会降低效率。
二 类型
Cursor类型包含三种: 隐式Cursor,显式Cursor和Ref Cursor(动态Cursor)。
1. 隐式Cursor:
1).对于Select …INTO…语句,一次只能从数据库中获取到一条数据,对于这种类型的DML Sql语句,就是隐式Cursor。例如:Select /Update / Insert/Delete操作。
2)作用:可以通过隐式Cusor的属性来了解操作的状态和结果,从而达到流程的控制。Cursor的属性包含:
SQL%ROWCOUNT 整型 代表DML语句成功执行的数据行数
SQL%FOUND 布尔型 值为TRUE代表插入、删除、更新或单行查询操作成功
SQL%NOTFOUND 布尔型 与SQL%FOUND属性返回值相反
SQL%ISOPEN 布尔型 DML执行过程中为真,结束后为假
3) 隐式Cursor是系统自动打开和关闭Cursor.
下面是一个Sample:
Set Serveroutput on;
begin
update t_contract_master set liability_state = 1 where policy_code = '123456789';
if SQL%Found then
dbms_output.put_line('the Policy is updated successfully.');
commit;
else
dbms_output.put_line('the policy is updated failed.');
end if;
end;
2. 显式Cursor:
(1) 对于从数据库中提取多行数据,就需要使用显式Cursor。显式Cursor的属性包含:
游标的属性 返回值类型 意 义
%ROWCOUNT 整型 获得FETCH语句返回的数据行数
%FOUND 布尔型 最近的FETCH语句返回一行数据则为真,否则为假
%NOTFOUND 布尔型 与%FOUND属性返回值相反
%ISOPEN 布尔型 游标已经打开时值为真,否则为假
(2) 对于显式游标的运用分为四个步骤:
定义游标---Cursor [Cursor Name] IS;
打开游标---Open [Cursor Name];
操作数据---Fetch [Cursor name]
关闭游标---Close [Cursor Name],这个Step绝对不可以遗漏。
(3)以下是三种常见显式Cursor用法。
Set serveroutput on;
declare
---define Cursor
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
curPolicyInfo cur_policy%rowtype;---定义游标变量
Begin
open cur_policy; ---open cursor
Loop
--deal with extraction data from DB
Fetch cur_policy into curPolicyInfo;
Exit when cur_policy%notfound;
Dbms_Output.put_line(curPolicyInfo.policy_code);
end loop;
Exception
when others then
close cur_policy;
Dbms_Output.put_line(Sqlerrm);
if cur_policy%isopen then
--close cursor
close cur_policy;
end if;
end;
/
------------------------
Set serveroutput on;
declare
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
v_policyCode t_contract_master.policy_code%type;
v_applicantId t_contract_master.applicant_id%type;
v_periodPrem t_contract_master.period_prem%type;
v_bankCode t_contract_master.bank_code%type;
v_bankAccount t_contract_master.bank_account%type;
Begin
open cur_policy;
Loop
Fetch cur_policy into v_policyCode,
v_applicantId,
v_periodPrem,
v_bankCode,
v_bankAccount;
Exit when cur_policy%notfound;
Dbms_Output.put_line(v_policyCode);
end loop;
Exception
when others then
close cur_policy;
Dbms_Output.put_line(Sqlerrm);
if cur_policy%isopen then
close cur_policy;
end if;
end;
/
--------------------------------
Set serveroutput on;
declare
Cursor cur_policy is
select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account
from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in ('2','3','4')
and rownum < 5
order by cm.policy_code desc;
Begin
For rec_Policy in cur_policy loop
Dbms_Output.put_line(rec_policy.policy_code);
end loop;
Exception
when others then
Dbms_Output.put_line(Sqlerrm);
end;
3. Ref Cursor(动态游标):
1) 与隐式Cursor,显式Cursor的区别:Ref Cursor是可以通过在运行期间传递参数来获取数据结果集。而另外两种Cursor,是静态的,在编译期间就决定数据结果集。
2) Ref cursor的使用:
Type [Cursor type name] is ref cursor
Define 动态的Sql语句
Open cursor
操作数据---Fetch [Cursor name]
Close Cursor
下面是一个Sample:
Set serveroutput on;
Declare
---define cursor type name
type cur_type is ref cursor;
cur_policy cur_type;
sqlStr varchar2(500);
rec_policy t_contract_master%rowtype;
begin
---define 动态Sql
sqlStr := 'select cm.policy_code, cm.applicant_id, cm.period_prem,cm.bank_code,cm.bank_account from t_contract_master cm
where cm.liability_state = 2
and cm.policy_type = 1
and cm.policy_cate in (2,3,4)
and rownum < 5
order by cm.policy_code desc ';
---Open Cursor
open cur_policy for sqlStr;
loop
fetch cur_policy into rec_policy.policy_code, rec_policy.applicant_id, rec_policy.period_prem,rec_policy.bank_code,rec_policy.bank_account;
exit when cur_policy%notfound;
Dbms_Output.put_line('Policy_code:'||rec_policy.policy_code);
end loop;
close cur_policy;
end;
4.常见Exception
1. 错 误 名 称 错误代码 错 误 含 义
2. CURSOR_ALREADY_OPEN ORA_06511 试图打开已经打开的游标
3. INVALID_CURSOR ORA_01001 试图使用没有打开的游标
4. DUP_VAL_ON_INDEX ORA_00001 保存重复值到惟一索引约束的列中
5. ZERO_DIVIDE ORA_01476 发生除数为零的除法错误
6. INVALID_NUMBER ORA_01722 试图对无效字符进行数值转换
7. ROWTYPE_MISMATCH ORA_06504 主变量和游标的类型不兼容
8. VALUE_ERROR ORA_06502 转换、截断或算术运算发生错误
9. TOO_MANY_ROWS ORA_01422 SELECT…INTO…语句返回多于一行的数据
10. NO_DATA_FOUND ORA_01403 SELECT…INTO…语句没有数据返回
11. TIMEOUT_ON_RESOURCE ORA_00051 等待资源时发生超时错误
12. TRANSACTION_BACKED_OUT ORA_00060 由于死锁,提交失败
13. STORAGE_ERROR ORA_06500 发生内存错误
14. PROGRAM_ERROR ORA_06501 发生PL/SQL内部错误
15. NOT_LOGGED_ON ORA_01012 试图操作未连接的数据库
16. LOGIN_DENIED ORA_01017 在连接时提供了无效用户名或口令
附:使用游标
15-1:使用标量变量接收游标数据
DECLARE
CURSOR emp_cursor IS
SELECT ename,job,sal FROM emp WHERE deptno=&dno;
v_ename emp.ename%TYPE;
v_sal emp.sal%TYPE;
v_job emp.job%TYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO v_ename,v_job,v_sal;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||v_ename||',岗位:'||v_job||',工资:'||v_sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-2:使用pl/sql纪录变量接收游标数据
DECLARE
CURSOR emp_cursor IS
SELECT ename,sal FROM emp ORDER BY sal DESC;
emp_record emp_cursor%ROWTYPE;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND OR emp_cursor%ROWCOUNT>&n;
dbms_output.put_line('姓名:'||emp_record.ename||
',工资:'||emp_record.sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-3:使用pl/sql集合变量接收游标数据
DECLARE
CURSOR emp_cursor IS SELECT ename,sal FROM emp
WHERE lower(job)=lower('&job');
TYPE emp_table_type IS TABLE OF emp_cursor%ROWTYPE
INDEX BY BINARY_INTEGER;
emp_table emp_table_type;
i INT;
BEGIN
OPEN emp_cursor;
LOOP
i:=emp_cursor%ROWCOUNT+1;
FETCH emp_cursor INTO emp_table(i);
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||emp_table(i).ename||
',工资:'||emp_table(i).sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-4:在游标for循环中引用已定义游标
DECLARE
CURSOR emp_cursor IS SELECT ename,hiredate FROM emp
ORDER BY hiredate DESC;
BEGIN
FOR emp_record IN emp_cursor LOOP
dbms_output.put_line('姓名:'||emp_record.ename
||',工作日期:'||emp_record.hiredate);
EXIT WHEN emp_cursor%ROWCOUNT=&n;
END LOOP;
END;
/
15-5:在游标for循环中直接引用子查询
BEGIN
FOR emp_record IN (SELECT ename,hiredate,rownum FROM emp
ORDER BY hiredate) LOOP
dbms_output.put_line('姓名:'||emp_record.ename
||',工作日期:'||emp_record.hiredate);
EXIT WHEN emp_record.rownum=&n;
END LOOP;
END;
/
15-6:参数游标
DECLARE
CURSOR emp_cursor(dno NUMBER) IS
SELECT ename,job FROM emp WHERE deptno=dno;
BEGIN
FOR emp_record IN emp_cursor(&dno) LOOP
dbms_output.put_line('姓名:'||emp_record.ename
||',岗位:'||emp_record.job);
END LOOP;
END;
/
15-7:更新游标行
DECLARE
CURSOR emp_cursor IS
SELECT ename,sal,deptno FROM emp FOR UPDATE;
dno INT:=&no;
BEGIN
FOR emp_record IN emp_cursor LOOP
IF emp_record.deptno=dno THEN
dbms_output.put_line('姓名:'||emp_record.ename
||',原工资:'||emp_record.sal);
UPDATE emp SET sal=sal*1.1 WHERE CURRENT OF emp_cursor;
END IF;
END LOOP;
END;
/
15-8:删除游标行
DECLARE
CURSOR emp_cursor IS
SELECT ename FROM emp FOR UPDATE;
name VARCHAR2(10):=lower('&name');
BEGIN
FOR emp_record IN emp_cursor LOOP
IF lower(emp_record.ename)=name THEN
DELETE FROM emp WHERE CURRENT OF emp_cursor;
ELSE
dbms_output.put_line('姓名:'||emp_record.ename);
END IF;
END LOOP;
END;
/
15-9:使用of子句在特定表上加行共享锁.
DECLARE
CURSOR emp_cursor IS
SELECT a.dname,b.ename FROM dept a JOIN emp b
ON a.deptno=b.deptno
FOR UPDATE OF b.deptno;
name VARCHAR2(10):=LOWER('&name');
BEGIN
FOR emp_record IN emp_cursor LOOP
IF LOWER(emp_record.dname)=name THEN
dbms_output.put_line('姓名:'||emp_record.ename);
DELETE FROM emp WHERE CURRENT OF emp_cursor;
END IF;
END LOOP;
END;
/
15-10:使用无返回类型的游标变量
DECLARE
TYPE ref_cursor_type IS REF CURSOR;
ref_cursor ref_cursor_type;
v1 NUMBER(6);
v2 VARCHAR2(10);
BEGIN
OPEN ref_cursor FOR
SELECT &col1 col1,&col2 col2 FROM &table WHERE &cond;
LOOP
FETCH ref_cursor INTO v1,v2;
EXIT WHEN ref_cursor%NOTFOUND;
dbms_output.put_line('col1='||v1||',col2='||v2);
END LOOP;
CLOSE ref_cursor;
END;
/
15-11:使用有返回类型的游标变量
DECLARE
TYPE emp_cursor_type IS REF CURSOR RETURN emp%ROWTYPE;
emp_cursor emp_cursor_type;
emp_record emp%ROWTYPE;
BEGIN
OPEN emp_cursor FOR SELECT * FROM emp
WHERE deptno=&dno;
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('姓名:'||emp_record.ename||
',工资:'||emp_record.sal);
END LOOP;
CLOSE emp_cursor;
END;
/
15-12:使用fetch...bulk collect提取所有数据
DECLARE
CURSOR emp_cursor IS
SELECT * FROM emp WHERE LOWER(job)=LOWER('&job');
TYPE emp_table_type IS TABLE OF emp%ROWTYPE;
emp_table emp_table_type;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor BULK COLLECT INTO emp_table;
CLOSE emp_cursor;
FOR i IN 1.. emp_table.COUNT LOOP
dbms_output.put_line('姓名:'||emp_table(i).ename
||',工资:'||emp_table(i).sal);
END LOOP;
END;
/
15-13:使用limit子句限制提取行数
DECLARE
CURSOR emp_cursor IS SELECT * FROM emp;
TYPE emp_array_type IS VARRAY(5) OF emp%ROWTYPE;
emp_array emp_array_type;
BEGIN
OPEN emp_cursor;
LOOP
FETCH emp_cursor BULK COLLECT INTO emp_array LIMIT &rows;
FOR i IN 1..emp_array.COUNT LOOP
dbms_output.put_line('姓名:'||emp_array(i).ename
||',工资:'||emp_array(i).sal);
END LOOP;
EXIT WHEN emp_cursor%NOTFOUND;
END LOOP;
CLOSE emp_cursor;
END;
/
15-14:使用cursor表达式
DECLARE
CURSOR dept_cursor(no NUMBER) IS
SELECT a.dname,CURSOR(SELECT * FROM emp
WHERE deptno=a.deptno)
FROM dept a WHERE a.deptno=no;
TYPE ref_cursor_type IS REF CURSOR;
emp_cursor ref_cursor_type;
emp_record emp%ROWTYPE;
v_dname dept.dname%TYPE;
BEGIN
OPEN dept_cursor(&dno);
LOOP
FETCH dept_cursor INTO v_dname,emp_cursor;
EXIT WHEN dept_cursor%NOTFOUND;
dbms_output.put_line('部门名:'||v_dname);
LOOP
FETCH emp_cursor INTO emp_record;
EXIT WHEN emp_cursor%NOTFOUND;
dbms_output.put_line('----雇员名:'||emp_record.ename
||',岗位:'||emp_record.job);
END LOOP;
END LOOP;
CLOSE dept_cursor;
END;