ORACLE 上机练习 理论部分: 1
动态游标的使用方法 前面所讲都是在变量声明部分定义的游标,它是静态的
不能在程序运行过程中修改
通过采用动态游标,可以在程序运行阶段,随时生成一个查询语句作为游标
使用动态游标步骤: 定义游标类型: TYPE 游标类型名 REF CURSOR; 声明游标变量: 游标变量名 游标类型名; 生成查询语句作为游标: OPEN 游标变量名 FOR 查询语句字符串; 【例】利用游标显示每个部门的工资总和和每个职位的平均工资
提示:动态生成两个游标 declare bm number; zw varchar2(10); tt number; str varchar2(50); type cur_type is ref cursor; --定义游标类型 cur cur_type; --定义游标变量 begin str:= 'select deptno,sum(sal) from emp group by deptno'; --查询字符串 open cur for str; dbms_output
put_line('每个部门的工资总和:'); u while cur%found loop dbms_output
put_line(bm||’ ‘||tt); fetch cur into bm,tt; end loop; str:= 'select job,avg(sal) from emp group by job’; --查询字符串 open cur for str; dbms_output
put_line('每个职位的工资总和:'); fetch cur into zw,tt; while cur%found loop dbms_output
put_line(zw||