当前位置:文档之家› Oracle经典练习题(很全面)

Oracle经典练习题(很全面)

Oracle 经典练习题一.创建一个简单的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表中显示该部门的名称与所在位置。

方法一:(传统方法)declarepname dept.dname%type;ploc dept.loc%type;pdeptno dept.deptno%type;beginpdeptno:=&请输入部门编号;select dname,loc into pname,ploc from dept where deptno=pdeptno; dbms_output.put_line('部门名称: '||pname||'所在位置:'||ploc); exception –异常处理when no_data_foundthen dbms_output.put_line('你输入的部门编号有误!!');when othersthen dbms_output.put_line('其他异常');end;方法二:(使用%rowtype)declareerow dept%rowtype;beginselect * into erow from dept where deptno=&请输入部门编号;dbms_output.put_line(erow.dname||'--'||erow.loc);exceptionwhen no_data_foundthen dbms_output.put_line('你输入的部门号有误');when othersthen dbms_output.put_line('其他异常');end;3.编写一个程序块,利用%type属性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。

declarepempno emp.empno%type;totalSal emp.sal%type;beginpempno:=&请输入员工编号;select sal+nvl(comm,0) into totalSal from emp where empno=pempno; dbms_output.put_line('该员工总共薪水'||totalSal);exceptionwhen no_data_foundthen dbms_output.put_line('你输入的员工编号有误!!');when othersthen dbms_output.put_line('其他异常');end;4.编写一个程序块,利用%rowtype属性,接受一个雇员号,从emp表中显示该雇员的整体薪水(即,薪水加佣金)。

declareerow emp%rowtype;beginselect * into erow from emp where empno=&请输入员工编号;dbms_output.put_line(erow.sal+nvl(m,0));exceptionwhen no_data_foundthen dbms_output.put_line('你输入的员工编号有误!!');when othersthen dbms_output.put_line('其他异常');end;5.某公司要根据雇员的职位来加薪,公司决定按下列加薪结构处理:Designation Raise-----------------------Clerk 500Salesman 1000Analyst 1500Otherwise 2000编写一个程序块,接受一个雇员名,从emp表中实现上述加薪处理。

declareerow emp%rowtype;beginselect * into erow from emp where ename='&name';if erow.job='Clerk'thenupdate emp set sal=sal+500where empno=erow.empno;elsif erow.job='Salesman'thenupdate emp set sal=sal+1000where empno=erow.empno;elsif erow.job='Analyst'thenupdate emp set sal=sal-1500where empno=erow.empno;elseupdate emp set sal=sal+2000where empno=erow.empno;end if;commit;exceptionwhen no_data_foundthen dbms_output.put_line('你输入的员工编号有误!!');when othersthen dbms_output.put_line('其他异常');end;6.编写一个程序块,将emp表中雇员名全部显示出来。

declarecursor cs is select ename from emp;beginfor erow in cs loopdbms_output.put_line(erow.ename);end loop;end;7.编写一个程序块,将emp表中前5人的名字显示出来。

方式一:declarecursor cs is select t.* from(select e.ename,rownum rm from emp e)t where t.rm between1and6;beginfor erow in cs loopdbms_output.put_line(erow.ename);end loop;end;方式二:--方式二declarecursor cs is select ename from emp;i number :=1;beginfor erow in cs loopdbms_output.put_line(erow.ename);i:=i+1; --迭代exit when i>5; --退出条件end loop;end;8.编写一个程序块,接受一个雇员名,从emp表中显示该雇员的工作岗位与薪水,若输入的雇员名不存在,显示“该雇员不存在”信息。

declarepjob emp.job%type;totalsal emp.sal%type;beginselect job,sal into pjob,totalsal from emp where ename='&请输入员工姓名';dbms_output.put_line(pjob ||'----' ||totalsal);exceptionwhen no_data_found thendbms_output.put_line('你输入的员工姓名有误!!');end;9.接受两个数相除并且显示结果,如果第二个数为0,则显示消息“除数不能为0”。

declarenum1 float;num2 float;res float;my_exception Exception;beginnum1:=&被除数;num2:=&除数;res:=num1/num2;raise my_exception;exceptionwhen my_exception thendbms_output.put_line(res);when others thendbms_output.put_line('除数不能为0');end;二.声明和使用游标------ 游标:(集合) ,处理返回多行记录的问题-- 声明游标--语法:cursor 游标名is DQL;-- 遍历游标/*1.打开游标, open 游标名;2.从游标中提取一行的记录:fetch 游标名into 变量名,...;3.使用循环,exit when 游标名%notfound;4.关闭游标,close 游标名;1.通过使用游标来显示dept表中的部门名称。

declarecursor co is select dname from dept;beginfor vname in coloopdbms_output.put_line(vname.dname);end loop;end;2.使用For循环,接受一个部门号,从emp表中显示该部门的所有雇员的姓名,工作和薪水。

declarecursor c_emp is select * from emp where deptno=&请输入部门号;beginfor erow in c_emp loopdbms_output.put_line(erow.ename ||' '||erow.job ||' '||erow.sal); end loop;exceptionwhen no_data_found thendbms_output.put_line('输入的部门编号有误');end;3.使用带参数的游标,实现第2题。

declarecursor c_cs(c_deptno number) is select* from emp where deptno=c_deptno; v_deptno number;beginv_deptno:=&请输入部门编号;for erow in c_cs(v_deptno) loopdbms_output.put_line(erow.ename ||' ' ||erow.job ||' '||erow.sal);end loop;exceptionwhen no_data_found thendbms_output.put_line('输入的部门编号有误');end;4.编写一个PL/SQL程序块,从emp表中对名字以“A”或“S”开始的所有雇员按他们基本薪水的10%给他们加薪。

相关主题