Oracle PL SQL经典练习题1 联系客服

发布时间 : 星期日 文章Oracle PL SQL经典练习题1更新完毕开始阅读cceee6b3fd0a79563c1e7230

Oracle 作业题

一.创建一个简单的PL/SQL程序块 使用不同的程序块组件工作 使用编程结构编写PL/SQL程序块 处理PL/SQL程序块中的错误

1.编写一个程序块,从emp表中显示名为“SMITH”的雇员的薪水和职位。

declare

v_emp emp%rowtype; begin

select * into v_emp from emp where ename='SMITH';

dbms_output.put_line('员工的工作是:'||v_emp.job||' ; 他的薪水是:'||v_emp.sal); end;

2.编写一个程序块,接受用户输入一个部门号,从dept表中显示该部门的名称与所在位置。 方法一:(传统方法)

declare

v_loc deptcp.dname%type; v_dname deptcp.dname%type; v_deptno deptcp.deptno%type; begin

v_deptno :=&部门编号;

select loc,dname into v_loc,v_dname from deptcp where deptno=v_deptno; dbms_output.put_line('员工所在地是:'||v_loc||';部门名称是:'||v_dname); exception

when no_data_found

then dbms_output.put_line('您输入的部门编号不存在,请从新输入,谢谢'); end;

方法二:(使用%rowtype)

declare

v_dept dept%rowtype; begin

select * into v_dept from dept where deptno=&部门号; dbms_output.put_line(v_dept.dname||'--'||v_dept.loc); end;

3.编写一个程序块,利用%type属性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。(*期末考试试题*)

declare

v_sal emp.sal%type; begin

select sal+comm into v_sal from emp where empno=&雇员号;

dbms_output.put_line(v_sal); end;

4.编写一个程序块,利用%rowtype属性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。

方式一:(错误程序)(让学生思考错在哪里?)

declare

v_emp empcp%rowtype; begin

select * into v_emp from empcp where empno = &雇员编号; dbms_output.put_line('整体薪水是:'||v_emp.sal+v_emp.comm); end;

declare

v_emp emp%rowtype; begin

select * into v_emp from emp where empno=&雇员号; dbms_output.put_line(v_emp.sal+v_emp.comm);

end;

5.某公司要根据雇员的职位来加薪,公司决定按下列加薪结构处理: Designation Raise ----------------------- Clerk 500 Salesman 1000

Analyst 1500 Otherwise 2000

编写一个程序块,接受一个雇员名,从emp表中实现上述加薪处理。(*期末考试试题*)

declare

v_emp emp%rowtype; begin

select * into v_emp from emp where ename='&name'; if v_emp.job='CLERK' then

update emp set sal=sal+500 where empno=v_emp.empno; elsif v_emp.job='SALESMAN' then

update emp set sal=sal+1000 where empno=v_emp.empno; elsif v_emp.job='ANALYST' then

update emp set sal=sal+1500 where empno=v_emp.empno; else

update emp set sal=sal+2000 where empno=v_emp.empno;

end if; commit; end;

6.编写一个程序块,将emp表中雇员名全部显示出来。

declare

cursor v_cursor is select * from emp; begin

for v_emp in v_cursor loop

dbms_output.put_line(v_emp.ename); end loop; end;

7.编写一个程序块,将emp表中前5人的名字显示出来。

declare

cursor v_cursor is select * from emp; v_count number :=1; begin

for v_emp in v_cursor loop

dbms_output.put_line(v_emp.ename); v_count := v_count+1; exit when v_count>5; end loop; end;

8.编写一个程序块,接受一个雇员名,从emp表中显示该雇员的工作岗位与薪水,若输入的雇员名不存在,显示“该雇员不存在”信息。(*期末考试试题*)

declare

v_emp emp%rowtype; my_exception Exception; begin

select * into v_emp from emp where ename='&name'; raise my_exception;

exception

when no_data_found then

dbms_output.put_line('该雇员不存在!'); when others then

dbms_output.put_line(v_emp.job||'---'||v_emp.sal); end;

9.接受两个数相除并且显示结果,如果第二个数为0,则显示消息“除数不能为0”(课堂未讲)。

declare

v_dividend float; v_divisor float; v_result float;

my_exception Exception; begin

v_dividend:=&被除数; v_divisor:=&除数;

v_result:=v_dividend/v_divisor; raise my_exception; exception

when my_exception then

dbms_output.put_line(v_result); when others then

dbms_output.put_line('除数不能为0'); end;

二.声明和使用游标 使用游标属性

使用游标For循环工作 声明带参数的游标

(使用FOR UPDATE OF和CURRENT OF子句工作)

1. 通过使用游标来显示dept表中的部门名称。

declare

cursor v_cursor is select * from dept; begin

for v_dept in v_cursor loop

dbms_output.put_line(v_dept.dname); end loop;