--游标(光标):当需要得到多行数据时,则需要游标
--游标的四大属性:%found//找到游标,%notfound//没有找到游标.%isopen【隐士游标,该属性值一直为false】,%rowcount//游标影响的数据条数
--游标的分类:显示游标,隐士游标,自定义游标(ref游标,动态游标)
--显示游标
--游标:四个步骤:定义游标--》打开游标--》使用游标---》关闭游标
--定义游标:
cursor 游标名字(参数)is select 语句
--打开游标
open 游标名字【(参数)】 ;
--使用游标
fetch 游标名字 into 变量;
--关闭游标
close 游标名
--for循环游标:省去打开,使用,关闭
for 记录型变量 in 游标名【(参数)】 loop
end loop;
--ref游标
declare
v char(1):='&input';
temp varchar(20);
--声明游标类型
type c_type is ref cursor;
--声明动态游标明亮
c c_type;
begin
if v='D' then
--打开游标
open c for select dname from dept;
elsif v='E' then
open c for select ename from emp;
end if;
fetch c into temp;
while c%found loop
dbms_output.put_line(temp);
fetch c into temp;
end loop;
--关闭游标
close c;
end;
--游标的注意事项:
--游标关闭的之前不能重复打开
--游标关闭之后可以重复关闭
--参数类型和返回值类型不能带长度
--实例
--运用标准cursor打印所有员工
declare
--定义游标
cursor cur is select ename,job from emp;
v_ename emp.ename%type;
v_job emp.job%type;
begin
--打开游标
open cur;
--使用游标
loop
fetch cur into v_ename,v_job;
exit when cur%notfound;
dbms_output.put_line(v_ename || ' ' || v_job);
end loop;
--关闭游标
close cur;
end;
declare
v_emp emp%rowtype;
cursor cur is select emp.* from emp;
--WHILE <布尔表达式> LOOP
-- 要执行的语句;
--END LOOP;
begin
open cur;
fetch cur into v_emp;
while cur%found loop
dbms_output.put_line(v_emp.ename || ' ' || v_emp.job);
fetch cur into v_emp;
end loop;
close cur;
end;
--运用for循环cursor打印所有员工
--方法一
declare
cursor cur is select * from emp;
begin
for emp in cur loop
dbms_output.put_line(emp.ename || ' ' || emp.job || ' ' || emp.deptno);
end loop;
end;
---方法二
begin
for emp in (select * from emp) loop
dbms_output.put_line(emp.ename || ' ' || emp.job || ' ' || emp.deptno);
end loop;
end;
--答应每个部门的部门编号和部门名称,以及该部门的所有员工信息
declare
--定义获得部门编号的cursor
cursor deptCursor is select * from dept;
--定义该部门下的所有员工信息的cursor
cursor empCursor(dno emp.deptno%type) is select * from emp where deptno =dno;
begin
for dep in deptCursor loop
dbms_output.put_line(dep.deptno || ' ' || dep.dname);
for em in empCursor(dep.deptno) loop
dbms_output.put_line(' '||em.ename || ' ' || em.deptno);
end loop;
end loop;
end;
--运用标准cursor打印20号员工
declare
--定义游标
cursor cur(dno emp.deptno%type) is select * from emp where deptno = dno;
e emp%rowtype;
begin
--打开cursor
open cur('&input');
--执行游标
fetch cur into e;
while cur%found loop
dbms_output.put_line(e.ename || ' ' || e.job);
fetch cur into e;
end loop;
--关闭游标
close cur;
end;
--运用动态cursor对话框传入一个值,如果是D,输出所有的部门名称,如果是E,输出所有的员工名称
declare
flag char(1):='&input';
tabelName varchar2(10);
type c_type is ref cursor;
--声明动态游标明亮
c c_type;
ep emp%rowtype;
dp dept%rowtype;
begin
--IF <布尔表达式> THEN
--PL/SQL 和 SQL语句
--END IF;
if flag ='D' or flag ='d' then
open c for select * from emp;
fetch c into ep;
while c%found loop
dbms_output.put_line(' '||ep.ename || ' ' || ep.deptno);
fetch c into ep;
end loop;
elsif flag = 'E' or flag = 'e' then
open c for select * from dept;
fetch c into dp;
while c%found loop
dbms_output.put_line(' '||dp.deptno || ' ' || dp.dname);
fetch c into dp;
end loop;
end if;
close c;
end;
--实例定义一个存储过程,用于将员工工资小于1000的员工的薪资添加100.
create or replace procedure pro2
is
v_sal emp.sal%type;
v_empno emp.empno%type;
--定义游标
cursor emp_cursor is select empno,sal from emp;
begin
--打开游标
open emp_cursor;
loop
fetch emp_cursor into v_empno,v_sal;
exit when emp_cursor%notfound;
if v_sal<1000 then
v_sal := v_sal + 100;
update emp set sal= v_sal where empno = v_empno;
end if;
dbms_output.put_line('no is' || v_empno || 'sal is' || v_sal );
end loop;
close emp_cursor;
end;
declare
begin
pro2();
end;
select * from emp;
--游标的四大属性:%found//找到游标,%notfound//没有找到游标.%isopen【隐士游标,该属性值一直为false】,%rowcount//游标影响的数据条数
--游标的分类:显示游标,隐士游标,自定义游标(ref游标,动态游标)
--显示游标
--游标:四个步骤:定义游标--》打开游标--》使用游标---》关闭游标
--定义游标:
cursor 游标名字(参数)is select 语句
--打开游标
open 游标名字【(参数)】 ;
--使用游标
fetch 游标名字 into 变量;
--关闭游标
close 游标名
--for循环游标:省去打开,使用,关闭
for 记录型变量 in 游标名【(参数)】 loop
end loop;
--ref游标
declare
v char(1):='&input';
temp varchar(20);
--声明游标类型
type c_type is ref cursor;
--声明动态游标明亮
c c_type;
begin
if v='D' then
--打开游标
open c for select dname from dept;
elsif v='E' then
open c for select ename from emp;
end if;
fetch c into temp;
while c%found loop
dbms_output.put_line(temp);
fetch c into temp;
end loop;
--关闭游标
close c;
end;
--游标的注意事项:
--游标关闭的之前不能重复打开
--游标关闭之后可以重复关闭
--参数类型和返回值类型不能带长度
--实例
--运用标准cursor打印所有员工
declare
--定义游标
cursor cur is select ename,job from emp;
v_ename emp.ename%type;
v_job emp.job%type;
begin
--打开游标
open cur;
--使用游标
loop
fetch cur into v_ename,v_job;
exit when cur%notfound;
dbms_output.put_line(v_ename || ' ' || v_job);
end loop;
--关闭游标
close cur;
end;
declare
v_emp emp%rowtype;
cursor cur is select emp.* from emp;
--WHILE <布尔表达式> LOOP
-- 要执行的语句;
--END LOOP;
begin
open cur;
fetch cur into v_emp;
while cur%found loop
dbms_output.put_line(v_emp.ename || ' ' || v_emp.job);
fetch cur into v_emp;
end loop;
close cur;
end;
--运用for循环cursor打印所有员工
--方法一
declare
cursor cur is select * from emp;
begin
for emp in cur loop
dbms_output.put_line(emp.ename || ' ' || emp.job || ' ' || emp.deptno);
end loop;
end;
---方法二
begin
for emp in (select * from emp) loop
dbms_output.put_line(emp.ename || ' ' || emp.job || ' ' || emp.deptno);
end loop;
end;
--答应每个部门的部门编号和部门名称,以及该部门的所有员工信息
declare
--定义获得部门编号的cursor
cursor deptCursor is select * from dept;
--定义该部门下的所有员工信息的cursor
cursor empCursor(dno emp.deptno%type) is select * from emp where deptno =dno;
begin
for dep in deptCursor loop
dbms_output.put_line(dep.deptno || ' ' || dep.dname);
for em in empCursor(dep.deptno) loop
dbms_output.put_line(' '||em.ename || ' ' || em.deptno);
end loop;
end loop;
end;
--运用标准cursor打印20号员工
declare
--定义游标
cursor cur(dno emp.deptno%type) is select * from emp where deptno = dno;
e emp%rowtype;
begin
--打开cursor
open cur('&input');
--执行游标
fetch cur into e;
while cur%found loop
dbms_output.put_line(e.ename || ' ' || e.job);
fetch cur into e;
end loop;
--关闭游标
close cur;
end;
--运用动态cursor对话框传入一个值,如果是D,输出所有的部门名称,如果是E,输出所有的员工名称
declare
flag char(1):='&input';
tabelName varchar2(10);
type c_type is ref cursor;
--声明动态游标明亮
c c_type;
ep emp%rowtype;
dp dept%rowtype;
begin
--IF <布尔表达式> THEN
--PL/SQL 和 SQL语句
--END IF;
if flag ='D' or flag ='d' then
open c for select * from emp;
fetch c into ep;
while c%found loop
dbms_output.put_line(' '||ep.ename || ' ' || ep.deptno);
fetch c into ep;
end loop;
elsif flag = 'E' or flag = 'e' then
open c for select * from dept;
fetch c into dp;
while c%found loop
dbms_output.put_line(' '||dp.deptno || ' ' || dp.dname);
fetch c into dp;
end loop;
end if;
close c;
end;
--实例定义一个存储过程,用于将员工工资小于1000的员工的薪资添加100.
create or replace procedure pro2
is
v_sal emp.sal%type;
v_empno emp.empno%type;
--定义游标
cursor emp_cursor is select empno,sal from emp;
begin
--打开游标
open emp_cursor;
loop
fetch emp_cursor into v_empno,v_sal;
exit when emp_cursor%notfound;
if v_sal<1000 then
v_sal := v_sal + 100;
update emp set sal= v_sal where empno = v_empno;
end if;
dbms_output.put_line('no is' || v_empno || 'sal is' || v_sal );
end loop;
close emp_cursor;
end;
declare
begin
pro2();
end;
select * from emp;