oracle pl sql经典练习题1

上传人:第*** 文档编号:30579880 上传时间:2018-01-30 格式:DOC 页数:9 大小:138.50KB
返回 下载 相关 举报
oracle   pl sql经典练习题1_第1页
第1页 / 共9页
oracle   pl sql经典练习题1_第2页
第2页 / 共9页
oracle   pl sql经典练习题1_第3页
第3页 / 共9页
oracle   pl sql经典练习题1_第4页
第4页 / 共9页
oracle   pl sql经典练习题1_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《oracle pl sql经典练习题1》由会员分享,可在线阅读,更多相关《oracle pl sql经典练习题1(9页珍藏版)》请在金锄头文库上搜索。

1、Oracle 作业题一.创建一个简单的 PL/SQL 程序块使用不同的程序块组件工作使用编程结构编写 PL/SQL 程序块处理 PL/SQL 程序块中的错误1.编写一个程序块,从 emp 表中显示名为“SMITH ”的雇员的薪水和职位。declarev_emp emp%rowtype;beginselect * into v_emp from emp where ename=SMITH;dbms_output.put_line(员工的工作是:|v_emp.job| ; 他的薪水是:|v_emp.sal);end;2.编写一个程序块,接受用户输入一个部门号,从 dept 表中显示该部门的名称与所

2、在位置。方法一:(传统方法)declarev_loc deptcp.dname%type;v_dname deptcp.dname%type;v_deptno deptcp.deptno%type;beginv_deptno :=select loc,dname into v_loc,v_dname from deptcp where deptno=v_deptno;dbms_output.put_line(员工所在地是:| v_loc|;部门名称是:| v_dname);exceptionwhen no_data_found then dbms_output.put_line(您输入的部门编

3、号不存在,请从新输入,谢谢 );end;方法二:(使用%rowtype )declarev_dept dept%rowtype;beginselect * into v_dept from dept where deptno=dbms_output.put_line(v_dept.dname|-|v_dept.loc);end;3.编写一个程序块,利用%type 属性,接受一个雇员号,从 emp 表中显示该雇员的整体薪水(即,薪水加佣金)。 (*期末考试试题*)declarev_sal emp.sal%type;begin select sal+comm into v_sal from emp

4、 where empno=dbms_output.put_line(v_sal);end;4.编写一个程序块,利用%rowtype 属性,接受一个雇员号,从 emp 表中显示该雇员的整体薪水(即,薪水加佣金)。方式一:(错误程序) (让学生思考错在哪里?)declarev_emp empcp%rowtype;beginselect * into v_emp from empcp where empno = dbms_output.put_line(整体薪水是:| v_emp.sal+v_m);end;declarev_emp emp%rowtype;beginselect * into v_e

5、mp from emp where empno=dbms_output.put_line(v_emp.sal+v_m);end;5.某公司要根据雇员的职位来加薪,公司决定按下列加薪结构处理:Designation Raise-Clerk 500Salesman 1000Analyst 1500Otherwise 2000编写一个程序块,接受一个雇员名,从 emp 表中实现上述加薪处理。 (*期末考试试题*)declare v_emp emp%rowtype;beginselect * into v_emp from emp where ename=if v_emp.job=CLERK then

6、update emp set sal=sal+500 where empno=v_emp.empno;elsif v_emp.job=SALESMAN thenupdate emp set sal=sal+1000 where empno=v_emp.empno; elsif v_emp.job=ANALYST thenupdate 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.编写一个程序块,

7、将 emp 表中雇员名全部显示出来。declarecursor v_cursor is select * from emp;beginfor v_emp in v_cursorloopdbms_output.put_line(v_emp.ename);end loop; end;7.编写一个程序块,将 emp 表中前 5 人的名字显示出来。declarecursor v_cursor is select * from emp;v_count number :=1;beginfor v_emp in v_cursorloopdbms_output.put_line(v_emp.ename);v_

8、count := v_count+1;exit when v_count5;end loop; end;8.编写一个程序块,接受一个雇员名,从 emp 表中显示该雇员的工作岗位与薪水,若输入的雇员名不存在,显示“该雇员不存在”信息。 (*期末考试试题*)declare v_emp emp%rowtype;my_exception Exception;begin select * into v_emp from emp where ename=raise my_exception;exceptionwhen no_data_found thendbms_output.put_line(该雇员不存

9、在! );when others thendbms_output.put_line(v_emp.job|-|v_emp.sal);end; 9.接受两个数相除并且显示结果,如果第二个数为 0,则显示消息“除数不能为 0”(课堂未讲)。declarev_dividend float;v_divisor float;v_result float;my_exception Exception;beginv_dividend:=v_divisor:=v_result:=v_dividend/v_divisor;raise my_exception;exceptionwhen my_exception

10、thendbms_output.put_line(v_result);when others thendbms_output.put_line(除数不能为0);end;二.声明和使用游标使用游标属性 使用游标 For 循环工作声明带参数的游标(使用 FOR UPDATE OF 和 CURRENT OF 子句工作)1. 通过使用游标来显示 dept 表中的部门名称。declarecursor v_cursor is select * from dept;beginfor v_dept in v_cursorloop dbms_output.put_line(v_dept.dname);end l

11、oop; end;2. 使用 For 循环,接受一个部门号,从 emp 表中显示该部门的所有雇员的姓名,工作和薪水。declarecursor v_cursor is select * from emp where deptno=beginfor v_emp in v_cursorloop dbms_output.put_line(v_emp.ename|-|v_emp.job|-|v_emp.sal);end loop;end;3. 使用带参数的游标,实现第 2 题。declarecursor v_cursor(p_deptno number) is select * from emp wh

12、ere deptno=p_deptno;v_deptno number(2);beginv_deptno:=for v_emp in v_cursor(v_deptno)loopdbms_output.put_line(v_emp.ename|-|v_emp.job|-|v_emp.sal);end loop;end; 4.编写一个 PL/SQL 程序块,从 emp 表中对名字以“A”或“S”开始的所有雇员按他们基本薪水的 10%给他们加薪。declarecursor v_cursor is select * from emp;beginfor v_emp in v_cursorloopif

13、v_emp.ename like A% thenupdate emp set sal=sal+sal*0.1 where empno=v_emp.empno;elsif v_emp.ename like S% thenupdate emp set sal=sal+sal*0.1 where empno=v_emp.empno;end if; commit;end loop;end;5. emp 表中对所有雇员按他们基本薪水的 10%给他们加薪,如果所增加后的薪水大于5000 卢布,则取消加薪。declarecursor v_cursor isselect * from emp;beginfor

14、 v_emp in v_cursor loopif v_emp.sal * 1.1 5000 thenupdate emp set sal = sal * 1.1 where empno = v_emp.empno;end if;commit;end loop;end;三,创建 PL/SQL 记录和 PL/SQL 表创建过程创建函数3.创建一个过程,能向 dept 表中添加一个新记录.(in 参数)create or replace procedureinsert_dept(dept_no in number,dept_name in varchar2,dept_loc in varchar2

15、)isbegininsert into dept values(dept_no,dept_name,dept_loc);end;调用该存储过程:begininsert_dept(50,技术部 ,武汉);end;4.创建一个过程,从 emp 表中带入雇员的姓名,返回该雇员的薪水值。 (out 参数)然后调用过程。create or replace procedurefind_emp3(emp_name in varchar2,emp_sal out number)is v_sal number(5);beginselect sal into v_sal from emp where ename = emp_name;emp_sal:=v_sal;exceptionwhen no_data_found thenemp_sal :=0;end;调用:declarev_sal number(5);beginfind_emp3(ALLEN,v_sal);dbms_output.put_line(v_sal);end;5.编写一个程序块,接受一个雇员号与一个百分数,从 emp 表中将该雇员的薪水增加输入的百分比(*课堂没讲) 。(利用过程,in out 参数)create or replace procedureupdate_sal(emp_no in number,par

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 外语文库 > 英语学习

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号