Oracle6 游标管理 作业
create user shihua identified by shihua;
grant connect,resource to shihua;
一、按下列要求完成
部门表
create table dept(
deptno varchar2(10) Primary key,--部门编号
dname varchar2(20), --部门名称
loc varchar2(200) --部门地址
);
员工表
create table emp(
empId varchar2(10) primary key,
ename varchar2(20), --姓名
deptno varchar2(10), --部门编号
job varchar2(20), --工种
hiredate date, --参加工作日期
sal number(8,2) --工资
)
1:任意执行一个 update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。
select sal from emp where empno = 7788;
update emp set sal = sal+10 where empno = 7788;
select sal from emp;
begin
update emp set sal = sal+10 where empno = 7788;
if
sql%isopen then dbms_output.put_line('isopen');
end if;
if
sql%found then dbms_output.put_line('found');
dbms_output.put_line(sql%rowcount);
end if;
if
sql%notfound then dbms_output.put_line('notfound');
end if;
exception
when no_data_found then dbms_output.put_line('no_data_found');
when too_many_rows then dbms_output.put_line('too_many_rows');
end;
begin
update emp set ENAME='ALEARK' WHERE EMPNO=7469;
if sql%isopen then
dbms_output.put_line('Openging');
else
dbms_output.put_line('closing');
end if;
if sql%found then
dbms_output.put_line('游标指向了有效行');
else
dbms_output.put_line('Sorry');
end if;
if sql%notfound then
dbms_output.put_line('Also Sorry');
else
dbms_output.put_line('Haha');
end if;
dbms_output.put_line(sql%rowcount);
exception
when no_data_found then
dbms_output.put_line('Sorry No data');
when too_many_rows then
dbms_output.put_line('Too Many rows');
end;
declare
empNumber emp.EMPNO%TYPE;
empName emp.ENAME%TYPE;
begin
if sql%isopen then
dbms_output.put_line('Cursor is opinging');
else
dbms_output.put_line('Cursor is Close');
end if;
if sql%notfound then
dbms_output.put_line('No Value');
else
dbms_output.put_line(empNumber);
end if;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line('-------------');
select EMPNO,ENAME into empNumber,empName from emp where EMPNO=7499;
dbms_output.put_line(sql%rowcount);
if sql%isopen then
dbms_output.put_line('Cursor is opinging');
else
dbms_output.put_line('Cursor is Closing');
end if;
if sql%notfound then
dbms_output.put_line('No Value');
else
dbms_output.put_line(empNumber);
end if;
exception
when no_data_found then
dbms_output.put_line('No Value');
when too_many_rows then
dbms_output.put_line('too many rows');
end;
2: 使用游标和loop循环来显示所有部门的名称
declare
v_dname dept.dname%type;
cursor dept_cur is select dname from dept;
begin
open dept_cur;
loop
fetch dept_cur into v_dname;
exit when dept_cur%notfound;
dbms_output.put_line(v_dname);
end loop;
close dept_cur;
end;
3:使用游标和while循环来显示所有部门的的地理位置(用%found属性)
declare
v_loc dept.loc%type;
cursor dept_cur is select loc from dept;
begin
open dept_cur;
fetch dept_cur into v_loc;
while dept_cur%found loop
dbms_output.put_line(v_loc);
fetch dept_cur into v_loc;
end loop;
end;
declare
cursor dept_cur is select * from dept;
begin
for deptinfo in dept_cur loop
dbms_output.put_line(deptinfo.deptno||' '||deptinfo.dname||' '||deptinfo.loc);
end loop;
end;
4:接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
declare
cursor emp_cur is select * from emp where deptno = '&deptno';
begin
for empinfo in emp_cur loop
dbms_output.put_line(empinfo.empno||' '||empinfo.ename||' '||empinfo.job||' '||empinfo.mgr||' '||empinfo.hiredate||' '||empinfo.sal||' '||empinfo.comm||' '||empinfo.deptno);
end loop;
end;
5:向游标传递一个工种,显示此工种的所有雇员的所有信息(使用参数游标)
declare
cursor c_job(p_job nvarchar2) is select * from emp where job=p_job;
r_job emp%rowtype;
begin
for r_job in c_job('CLERK') loop
dbms_output.put_line('员工号'||r_job.EMPNO||''||'员工姓名'||r_job.ENAME);
end loop;
end;
带参数的显式游标
declare
v_emp emp%rowtype;
cursor emp_cur(v_deptno emp.deptno%type,v_empno emp.empno%type)
is select * from emp where deptno=v_deptno and v_empno=v_empno;
begin
open emp_cur(&deptno,&empno);
loop
fetch emp_cur into v_emp;
exit when emp_cur%notfound;
dbms_output.put_line(v_emp.ename);
dbms_output.put_line(v_emp.sal);
end loop;
close emp_cur;
end;
create user shihua identified by shihua;
grant connect,resource to shihua;
一、按下列要求完成
部门表
create table dept(
deptno varchar2(10) Primary key,--部门编号
dname varchar2(20), --部门名称
loc varchar2(200) --部门地址
);
员工表
create table emp(
empId varchar2(10) primary key,
ename varchar2(20), --姓名
deptno varchar2(10), --部门编号
job varchar2(20), --工种
hiredate date, --参加工作日期
sal number(8,2) --工资
)
1:任意执行一个 update操作,用隐式游标sql的属性%found,%notfound,%rowcount,%isopen观察update语句的执行情况。
select sal from emp where empno = 7788;
update emp set sal = sal+10 where empno = 7788;
select sal from emp;
begin
update emp set sal = sal+10 where empno = 7788;
if
sql%isopen then dbms_output.put_line('isopen');
end if;
if
sql%found then dbms_output.put_line('found');
dbms_output.put_line(sql%rowcount);
end if;
if
sql%notfound then dbms_output.put_line('notfound');
end if;
exception
when no_data_found then dbms_output.put_line('no_data_found');
when too_many_rows then dbms_output.put_line('too_many_rows');
end;
begin
update emp set ENAME='ALEARK' WHERE EMPNO=7469;
if sql%isopen then
dbms_output.put_line('Openging');
else
dbms_output.put_line('closing');
end if;
if sql%found then
dbms_output.put_line('游标指向了有效行');
else
dbms_output.put_line('Sorry');
end if;
if sql%notfound then
dbms_output.put_line('Also Sorry');
else
dbms_output.put_line('Haha');
end if;
dbms_output.put_line(sql%rowcount);
exception
when no_data_found then
dbms_output.put_line('Sorry No data');
when too_many_rows then
dbms_output.put_line('Too Many rows');
end;
declare
empNumber emp.EMPNO%TYPE;
empName emp.ENAME%TYPE;
begin
if sql%isopen then
dbms_output.put_line('Cursor is opinging');
else
dbms_output.put_line('Cursor is Close');
end if;
if sql%notfound then
dbms_output.put_line('No Value');
else
dbms_output.put_line(empNumber);
end if;
dbms_output.put_line(sql%rowcount);
dbms_output.put_line('-------------');
select EMPNO,ENAME into empNumber,empName from emp where EMPNO=7499;
dbms_output.put_line(sql%rowcount);
if sql%isopen then
dbms_output.put_line('Cursor is opinging');
else
dbms_output.put_line('Cursor is Closing');
end if;
if sql%notfound then
dbms_output.put_line('No Value');
else
dbms_output.put_line(empNumber);
end if;
exception
when no_data_found then
dbms_output.put_line('No Value');
when too_many_rows then
dbms_output.put_line('too many rows');
end;
2: 使用游标和loop循环来显示所有部门的名称
declare
v_dname dept.dname%type;
cursor dept_cur is select dname from dept;
begin
open dept_cur;
loop
fetch dept_cur into v_dname;
exit when dept_cur%notfound;
dbms_output.put_line(v_dname);
end loop;
close dept_cur;
end;
3:使用游标和while循环来显示所有部门的的地理位置(用%found属性)
declare
v_loc dept.loc%type;
cursor dept_cur is select loc from dept;
begin
open dept_cur;
fetch dept_cur into v_loc;
while dept_cur%found loop
dbms_output.put_line(v_loc);
fetch dept_cur into v_loc;
end loop;
end;
declare
cursor dept_cur is select * from dept;
begin
for deptinfo in dept_cur loop
dbms_output.put_line(deptinfo.deptno||' '||deptinfo.dname||' '||deptinfo.loc);
end loop;
end;
4:接收用户输入的部门编号,用for循环和游标,打印出此部门的所有雇员的所有信息(使用循环游标)
declare
cursor emp_cur is select * from emp where deptno = '&deptno';
begin
for empinfo in emp_cur loop
dbms_output.put_line(empinfo.empno||' '||empinfo.ename||' '||empinfo.job||' '||empinfo.mgr||' '||empinfo.hiredate||' '||empinfo.sal||' '||empinfo.comm||' '||empinfo.deptno);
end loop;
end;
5:向游标传递一个工种,显示此工种的所有雇员的所有信息(使用参数游标)
declare
cursor c_job(p_job nvarchar2) is select * from emp where job=p_job;
r_job emp%rowtype;
begin
for r_job in c_job('CLERK') loop
dbms_output.put_line('员工号'||r_job.EMPNO||''||'员工姓名'||r_job.ENAME);
end loop;
end;
带参数的显式游标
declare
v_emp emp%rowtype;
cursor emp_cur(v_deptno emp.deptno%type,v_empno emp.empno%type)
is select * from emp where deptno=v_deptno and v_empno=v_empno;
begin
open emp_cur(&deptno,&empno);
loop
fetch emp_cur into v_emp;
exit when emp_cur%notfound;
dbms_output.put_line(v_emp.ename);
dbms_output.put_line(v_emp.sal);
end loop;
close emp_cur;
end;