游标的属性

  1. 游标分类  
  2. 显式游标:使用cursor语句显式定义游标,游标被定义后,需要打开并提取游标,关闭游标。  
  3. 隐式游标:由oracle为每一个dml语句创建一个隐式游标也叫做SQL游标。  
  4.   
  5. BEGIN  
  6.   update emp  
  7.     set comm = comm *1.12  
  8.     where empno = 7346;  
  9.   dbms_output.put_line(SQL%ROWCOUNT || '行被更新');--使用隐式游标  
  10.   if sql%notfound --使用隐式游标  
  11.   then  
  12.     dbms_output.put_line('不能更新员工号为7346的员工');  
  13.   end if;  
  14.   commit;  
  15. end;  
  16. --由于隐式游标会自动关闭,因此SQL%isopen都是false.  
  17.   
  18. declare  
  19.   cursor emp_cursor (p_deptno in number) return emp%rowtype --定义游标并指定参数 和返回值  
  20.   is   
  21.     select * from emp where deptno = p_deptno;  
  22. begin  
  23.   open emp_cursor(20);  
  24. end;  
  25.   
  26. 游标属性  
  27. %ISOPEN:判断对应的游标变量是否打开,如果打开则返回true否则返回false.  
  28. declare   
  29.   cursor emp_cursor (p_deptno in number)  
  30.   is   
  31.     select * from emp where deptno = p_deptno;  
  32. begin  
  33.   if not emp_cursor%isopen then open emp_cursor(20);  
  34.   end if;  
  35.   if emp_cursor%isopen then   
  36.     dbms_output.put_line('游标已经被打开');  
  37.   else  
  38.     dbms_output.put_line('游标还未被打开');  
  39.   end if;  
  40.   close emp_cursor;  
  41. end;  
  42.   
  43. %FOUND:当游标被打开后,在调用fetch语句获取数据之前%found会产生NULL值。而此后每取得一行  
  44.         数据,其值会为true,如果获取数据失败返回false,因此%FOUND的作用是检查是否从结果集中获取到了数据.  
  45.           
  46. declare  
  47.   emp_row emp%rowtype;  
  48.   cursor emp_cursor (p_deptno in number)  
  49.   is select * from emp where deptno=p_deptno;  
  50. begin  
  51.   if not emp_cursor%ISOPEN  
  52.   then open emp_cursor(20);  
  53.   end if;  
  54.   if emp_cursor%found is null  
  55.   then   
  56.     dbms_output.put_line('%found 属性为null');--在fetch获取数据之前%found为null.  
  57.   end if;  
  58.   loop  
  59.     fetch emp_cursor into emp_row;  
  60.     exit when not emp_cursor%found; --exit when emp_cursor%notfound  
  61.   end loop;  
  62.   close em_cursor;  
  63. end;  
  64.   
  65. %notfound:此属性与%found相反。  
  66.   
  67. %rowcount:用来返回到目前为止已经从游标中取出的记录的行数,当游标被打开时,%rowcount值为0,  
  68.            每取得一条数据,%rowcount的值就加1.  
  69. declare   
  70.   emp_row emp%rowtype;  
  71.   cursor emp_cursor (p_deptno in number)  
  72.   is select * from emp where deptno=p_deptno;  
  73. begin  
  74.   open emp_cursor(20);  
  75.   loop   
  76.     fetch emp_cursor into emp_row;  
  77.     exit when emp_cursor%notfound;  
  78.     dbms_output.put_line('当前已提取的行数为:'||emp_cursor%rowcount||'行!');  
  79.   end loop;  
  80.   close emp_cursor;  
  81. end; 
posted @ 2016-11-04 10:14  SpringMVCMaven  阅读(1111)  评论(0编辑  收藏  举报