cursor of sql in pl / sql

1. Grammar 1 [Basic cursor format]

--创建游标
cursor 游标名称 is sql语句
--打开游标
open 游标名称;
--遍历的数据存放到emprow记录型变量,fetch获取游标的数据
fetch cl into emprow;
--查询没有数据
%notfound

2. Examples

image

3. Syntax 2 [Cursor format with parameters]

declare
  cursor cl2(eno emp.deptno%type)--对游标中存储的指定的参数进行数据类型设置
  is select empno from emp where deptno = eno;
  
  en emp.empno%type;
begin
  open cl2(10);--获取游标中指定参数的数据,需要打开游标时进行声明
       loop
         fetch cl2 into en;--读取游标的数据存放在
         exit when cl2%notfound;--设置退出循环条件
         update emp set sal=sal+100 where empno = en;--设置更新sql语句
         commit;--增删改需要进行事务提交
       end loop;
  close cl2;
end;

4. Examples

image

Published 62 original articles · won praise 20 · views 6775

Guess you like

Origin blog.csdn.net/qq_45421186/article/details/105464012