第三章 Oracle游标管理.docVIP

  • 2
  • 0
  • 约3.19千字
  • 约 9页
  • 2017-06-13 发布于湖北
  • 举报
Oracle游标管理 一、目标 掌握游标管理技巧 游标概念 游标(cursor)也叫光标.在PL/SQL程序中使用游标语句和select语句一起对表或视图中的数据进行查询并逐条进行读取 二、游标简介 1、游标功能 2、游标分类 逐行处理查询结果,以编程的方式访问数据 游标类型: 隐式游标:在PL/SQL程序中执行DML SQL语句是自动创建隐式游标 显示游标:用于处理返回多行的查询 静态游标:相当于查询整个游标中的记录,二游标变量定义一个记录集,给予调用 REF游标(游标变量(动态游标))用于处理运行是才能确定的动态SQL查询的结果 三、隐式游标 1、在PL/SQL中使用DML语句是自动创建隐式游标 2、隐式游标自动声明、打开和关闭,其名为SQL 3、通过检查隐式游标的属性可以获得最近执行的DML语句的信息 4、隐式游标的属性有: %Found – SQL语句影响了一行或多行时为TRUE 针对Scott.emp表中数据进行修改 begin update scott.emp set sal=sal+500 where empno=7876; if sql%found then dbms_output.put_line(表以更新); end if; end; / %Notfound – SQL 语句没有影响任何行时为TRUE declare emp_id scott.emp.empno%type := empno; emp_ename scott.emp.ename%type := ename; begin update scott.emp set ename=emp_ename where empno=emp_id; if sql%notfound then dbms_output.put_line(编号不存在!); else dbms_output.put_line(表以更新); end if; end; / %Rowcount – SQL 语句影响的行数 begin update scott.emp set ename=qiqi where empno=7876; dbms_output.put_line(sql%rowcount); end; / begin update scott.emp set sal=sal+500 where sal=3000; dbms_output.put_line(sql%rowcount); end; / %Isopen – SQL游标是否打开,始终为FALSE 5、这里的功能是对隐式游标的调用来实现的 NO_DATA_FOUND-------没有数据 declare empid scott.emp.empno%type; desig scott.emp.sal%type; begin empid := empnumber; select sal into desig from scott.emp where empno=empid; exception when NO_DATA_FOUND then dbms_output.put_line(职员不存在); end; / TOO_MANY_ROWS-----------查询了多行 declare empid scott.emp.empno%type; begin select empno into empid from scott.emp; exception when TOO_MANY_ROWS then dbms_output.put_line(该查询提取了多行); end; / 四、显式游标 1、显示游标在PL/SQL块的声明部分定义查询,该查询可以返回多行 2、显示游标的操作过程: 3、显式游标例子 declare personnel_sal scott.emp.sal%type; personnel_name scott.emp.ename%type; CURSOR sal_cur is select ename,sal from scott.emp where sal2000;-------声明游标 begin open sal_cur;-------打开游标 loop fetch sal_cur into personnel_name,personnel_sal;---------提取游标中的行 exit when sal_cur%NOTFOUND; dbms_output.put_line (personnel_name||员工工资是=:||personnel_sal); end loop; close sal_cu

文档评论(0)

1亿VIP精品文档

相关文档