--用pl/sql写一个块,输入部门号,显示该部门所有员工的name,sal
set serveroutput on;--打开控制台输出
declare
--定义游标类型 sp_emp_cursor
type sp_emp_cursor is ref cursor;
--定义一个游标变量
test_cursor sp_emp_cursor;
--定义变量
v_ename emp.ename%type;
v_sal emp.sal%type;
begin
--把test_cursor和一个select结合
open test_cursor for select ename,sal from emp where deptno=&no;
--循环取出
loop
fetch test_cursor into v_ename,v_sal;
--判断是否test_cursor为空
exit when test_cursor%notfound;
dbms_output.put_line('名字:'||v_ename||'工资:'||v_sal);
end loop;
--关闭游标
close test_cursor;
end;
/* 结果:
名字:ALLEN工资:1600
名字:WARD工资:1250
名字:MARTIN工资:1250
名字:BLAKE工资:2850
名字:TURNER工资:1500
名字:JAMES工资:950
*/
set serveroutput on;--打开控制台输出
declare --声明
v_name varchar2(5);--变量1
v_sal number(7,2);--变量2
begin
select ename,sal into v_name,v_sal from emp where empno=ⅆ
dbms_output.put_line('用户名:'||v_name || '工资:'||v_sal);--在控制台输出
--异常处理
exception
when no_data_found --当发生了no_data_found异常
then dbms_output.put_line('输入有误');--然后处理机制
end;