Oracle经典练习题(很全面)(共10页).doc
《Oracle经典练习题(很全面)(共10页).doc》由会员分享,可在线阅读,更多相关《Oracle经典练习题(很全面)(共10页).doc(10页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上Oracle 经典练习题一.创建一个简单的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 pname dept
2、.dname%type; ploc dept.loc%type; pdeptno dept.deptno%type;begin pdeptno:=&请输入部门编号; select dname,loc into pname,ploc from dept where deptno=pdeptno; dbms_output.put_line(部门名称: |pname|所在位置:|ploc); exception 异常处理 when no_data_found then dbms_output.put_line(你输入的部门编号有误!); when others then dbms_output.pu
3、t_line(其他异常);end;方法二:(使用%rowtype) declare erow dept%rowtype; begin select * into erow from dept where deptno=&请输入部门编号; dbms_output.put_line(erow.dname|-|erow.loc); exception when no_data_found then dbms_output.put_line(你输入的部门号有误!); when others then dbms_output.put_line(其他异常); end; 3.编写一个程序块,利用%type属
4、性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。declare pempno emp.empno%type; totalSal emp.sal%type;begin pempno:=&请输入员工编号; select sal+nvl(comm,0) into totalSal from emp where empno=pempno; dbms_output.put_line(该员工总共薪水|totalSal); exception when no_data_found then dbms_output.put_line(你输入的员工编号有误!); when others
5、then dbms_output.put_line(其他异常);end; 4.编写一个程序块,利用%rowtype属性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。declare erow emp%rowtype;begin select * into erow from emp where empno=&请输入员工编号; dbms_output.put_line(erow.sal+nvl(m,0); exception when no_data_found then dbms_output.put_line(你输入的员工编号有误!); when others then
6、 dbms_output.put_line(其他异常);end; 5.某公司要根据雇员的职位来加薪,公司决定按下列加薪结构处理: Designation Raise - Clerk 500 Salesman 1000 Analyst 1500 Otherwise 2000编写一个程序块,接受一个雇员名,从emp表中实现上述加薪处理。declareerow emp%rowtype;begin select * into erow from emp where ename=&name; if erow.job=Clerk then update emp set sal=sal+500 where
7、empno=erow.empno; elsif erow.job=Salesman then update emp set sal=sal+1000 where empno=erow.empno; elsif erow.job=Analyst then update emp set sal=sal-1500 where empno=erow.empno; else update emp set sal=sal+2000 where empno=erow.empno; end if; commit;exception when no_data_found then dbms_output.put
8、_line(你输入的员工编号有误!); when others then dbms_output.put_line(其他异常);end;6.编写一个程序块,将emp表中雇员名全部显示出来。declare cursor cs is select ename from emp;begin for erow in cs loop dbms_output.put_line(erow.ename); end loop;end; 7.编写一个程序块,将emp表中前5人的名字显示出来。方式一:declare cursor cs is select t.* from (select e.ename,rownu
9、m rm from emp e)t where t.rm between 1 and 6;begin for erow in cs loop dbms_output.put_line(erow.ename); end loop;end;方式二:-方式二declare cursor cs is select ename from emp; i number :=1;begin for erow in cs loop dbms_output.put_line(erow.ename); i:=i+1; -迭代 exit when i5; -退出条件 end loop;end; 8.编写一个程序块,接
10、受一个雇员名,从emp表中显示该雇员的工作岗位与薪水,若输入的雇员名不存在,显示“该雇员不存在”信息。declare pjob emp.job%type; totalsal emp.sal%type;begin select job,sal into pjob,totalsal from emp where ename=&请输入员工姓名; dbms_output.put_line(pjob |- |totalsal); exception when no_data_found then dbms_output.put_line(你输入的员工姓名有误!);end; 9.接受两个数相除并且显示结果
11、,如果第二个数为0,则显示消息“除数不能为0”。declare num1 float; num2 float; res float; my_exception Exception; begin num1:=&被除数; num2:=&除数; res:=num1/num2; raise my_exception; exception when my_exception then dbms_output.put_line(res); when others then dbms_output.put_line(除数不能为0); end;二.声明和使用游标- 游标:(集合) ,处理返回多行记录的问题 -
12、 声明游标 -语法: cursor 游标名 is DQL;- 遍历游标 /* 1.打开游标, open 游标名; 2.从游标中提取一行的记录:fetch 游标名 into 变量名,.; 3.使用循环, exit when 游标名%notfound; 4.关闭游标, close 游标名;1. 通过使用游标来显示dept表中的部门名称。 declare cursor co is select dname from dept; begin for vname in co loop dbms_output.put_line(vname.dname); end loop; end;2. 使用For循环,
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Oracle 经典 练习题 全面 10
限制150内