; //退出条件 dbms_output.put_line(emp_row.ename); //输出结果 end loop; //退出循环 close ecname; //关闭游标变量 dbms_output.put_line('结束'); end; /
开始
MILLER
结束
//复杂的案例 SQL> declare type emp_cname is ref cursor return emp%rowtype; ecname emp_cname; emp_row emp%rowtype; begin dbms_output.put_line('开始'); open ecname for select * from emp; loop fetch ecname into emp_row; exit when ecname%notfound; dbms_output.put_line(emp_row.ename); end loop; close ecname; dbms_output.put_line('结束'); end; /
开始
SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER
结束
定位游标之后进行删除|修改指定的数据行 更新的时候需要使用for update选项。
Cursor cursor_name is select_statement; For update[of column[,column],[nowait]]
of用来指定要锁定的列,如果忽略of那么表中选择的数据行都将锁定。如果被锁定行已经被锁定了,那么必须等待释放才能锁定对于这种情况我们可以使用nowait语句。
当使用for update语句声明游标后,可以再delete|update语句中使用where current of子句,修改|删除游标结果集中当前行对应的表中的数据。
语法:
where { current of cursor_name|search_condition}
举例说明:
//修改操作 SQL> declare cursor ecname is select * from emp where empno=7934 for update of sal nowait; esal number(7,2); begin dbms_output.put_line('开始'); for r in ecname loop esal:=r.sal*10; update emp set sal=esal where current of ecname; end loop; dbms_output.put_line('结束'); end; /
开始
结束