declare v_emp emp%rowtype; begin select * into v_emp from emp where ename = 'SMITH'; if sql%found then dbms_output.put_line('有数据'); end if; end;
declare v_emp emp%rowtype; begin select * into v_emp from emp where ename = 'XIAOHONG';---从这一步开始报错,没机会走游标 if sql%found then dbms_output.put_line('有数据'); else dbms_output.put_line('无数据'); end if; end;
declare v_emp emp%rowtype; begin select * into v_emp from emp where ename = 'SMITH'; if sql%found then dbms_output.put_line(sql%rowcount); end if; end;
begin delete from empa; dbms_output.put_line(sql%rowcount); end;
select * from empa;
显示游标–用户可以自己声明跟管理的游标,需要自己起名字
1.声明游标 cursor_name is select 语句–游标名后面可以直接跟参数,作用就是传递参数 2.打开游标 open cursor_name–游标名 3.传递参数 fetch cursor_name–游标名 4.关闭游标 close cursor_name–游标名
declare cursor cursor_emp is select * from emp; v_emp emp%rowtype; begin open cursor_emp; fetch cursor_emp into v_emp;---直接使用的时候只能获取第一条,想要获取多条需要放在循环中 close cursor_emp; dbms_output.put_line(v_emp.ename||' '||v_emp.sal); end;
declare cursor cursor_emp is select * from emp; v_emp emp%rowtype; begin open cursor_emp; loop fetch cursor_emp into v_emp; ---直接使用的时候只能获取第一条,想要获取多条需要放在循环中 exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp is select * from emp where deptno = 10; v_emp emp%rowtype; begin open cursor_emp; loop fetch cursor_emp into v_emp; ---直接使用的时候只能获取第一条,想要获取多条需要放在循环中 exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp(bm number) is select * from emp where deptno = bm; v_emp emp%rowtype; begin open cursor_emp(&部门编号); loop fetch cursor_emp into v_emp; exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp(bm number) is select * from emp where deptno = bm; --游标名中,需要传递的数据类型名需要自己命,不能跟字段名一样 --游标中需要传递的数据类型要保持一致 v_emp emp%rowtype; begin open cursor_emp(10); loop fetch cursor_emp into v_emp; exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp(bm number,xm varchar2) is select * from emp where deptno = bm and ename = xm; --游标名中,需要传递的数据类型名需要自己命,不能跟字段名一样 --游标中需要传递的数据类型要保持一致 v_emp emp%rowtype; begin open cursor_emp(20,'SMITH'); loop fetch cursor_emp into v_emp; exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp(bm1 number,bm2 number) is select * from emp where deptno = bm1 or deptno = bm2; v_emp emp%rowtype; begin open cursor_emp(20,10);--即使传递同字段同类型的参数,每个参数也需要对应属于自己的数据类型 loop fetch cursor_emp into v_emp; exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
declare cursor cursor_emp(bm number) is select * from emp where deptno = bm; --游标名中,需要传递的数据类型名需要自己命,不能跟字段名一样 --游标中需要传递的数据类型要保持一致 v_emp emp%rowtype; begin open cursor_emp(in(10,20));--报错,不能使用集合一次传递多个参数 loop fetch cursor_emp into v_emp; exit when cursor_emp%notfound; dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; close cursor_emp; end;
在for循环中使用游标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
declare cursor cursor_emp is select * from emp; v_emp emp%rowtype; begin for v_emp in cursor_emp loop dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; end;
declare cursor cursor_emp(bm number) is select * from emp where deptno = bm; v_emp emp%rowtype; begin for v_emp in cursor_emp(&部门编号)loop dbms_output.put_line(v_emp.ename || ' ' || v_emp.sal); end loop; end;
for循环在使用游标时,会自动进行打开,传递,关闭游标的过程 open fetch close
1.当进入循环时,游标 for 循环语句自动打开游标并提取第一行游标数据; 2.当程序处理完当前所提取的数据而进入下一次循环时,游标for循环语句自动提取下一行数据供程序处理; 3.当提取完 结果集中全部数据后,结束循环,并自动关闭循环;