Oracle存储过程及函数的练习题
--存储过程、函数练习题
--(1)创建一个存储过程,以员工号为参数,输出该员工的工资
create or replace procedure p_sxt1(v_empno in emp.empno%type, v_sal out emp.sal%type) is
begin
select sal into v_sal from emp where empno = v_empno;
end;
--(1)执行
declare
v_empno emp.empno%type := 7369;
v_sal emp.sal%type;
begin
p_sxt1(v_empno,v_sal);
dbms_output.put_line(v_empno || ' 员工的工资为:' || v_sal);
end;
--(2)创建一个存储过程,以员工号为参数,修改该员工的工资。若该员工属于10号部门,
--则工资增加150;若属于20号部门,则工资增加200;若属于30号部门,则工资增加250;
--若属于其他部门,则增加300。
create or replace procedure p_sxt2(v_empno in emp.empno%type) is
v_deptno emp.deptno%type;
v_sal emp.sal%type;
begin
select deptno into v_deptno from emp where empno = v_empno;
select sal into v_sal from emp where empno = v_empno;
dbms_output.put_line(v_empno || ' 的部门是 ' || v_deptno || ' 修改前的工资是 ' || v_sal);
case v_deptno
when 10 then
update emp set sal = sal + 150 where empno = v_empno;
when 20 then
update emp set sal = sal + 200 where empno = v_empno;
when 30 then
update emp set sal = sal + 250 where empno = v_empno;
else
update emp set sal = sal + 300 where empno = v_empno;
end case;
select sal into v_sal from emp where empno = v_empno;
dbms_output.put_line(v_empno || ' 的部门是 ' || v_deptno || ' 修改后的工资是 ' || v_sal);
commit;
end;
--(2)执行
begin
p_sxt2(7369);
end;
--(3)创建一个存储过程,以员工号为参数,返回该员工的工作年限(以参数形式返回)。
create or replace procedure p_sxt3(v_empno in emp.empno%type, v_year out number) is
begin
select round((sysdate - hiredate)/365,1) into v_year from emp where empno = v_empno;
end;
--(3)执行
declare
v_empno emp.empno%type := 7369;
v_year number;
begin
p_sxt3(v_empno,v_year);
dbms_output.put_line(v_empno || ' 工作年限为 ' || v_year || '年');
end;
--(4)创建一个存储过程,以部门号为参数,输出入职日期最早的10个员工信息。
create or replace procedure p_sxt4(v_deptno emp.deptno%type) is
cursor c_emp is select * from emp where deptno = v_deptno order by hiredate;
v_times number := 0;
begin
for v_emp in c_emp loop
v_times := v_times + 1;
dbms_output.put_line(v_emp.empno || '**' || v_emp.ename || '**' || to_char(v_emp.hiredate,'yyyy-mm-dd'));
if v_times = 10 then
exit;
end if;
end loop;
end;
--(4)执行
begin
p_sxt4(20);
end;
--(5)创建一个函数,以员工号为参数,返回该员工的工资。
create or replace function f_sxt5(v_empno emp.empno%type) return emp.sal%type is
vr_sal emp.sal%type;
begin
select sal into vr_sal from emp where empno = v_empno;
return vr_sal;
end;
--(5)执行
select f_sxt5(7369)||'元' 工资 from dual;
--(6)创建一个函数,以部门号为参数,返回该部门的平均工资。
create or replace function f_sxt6(v_deptno emp.deptno%type) return emp.sal%type is
vr_sal emp.sal%type;
begin
select avg(sal) into vr_sal from emp where deptno = v_deptno;
return vr_sal;
end;
--(6)执行
select f_sxt6(20) 部门平均工资 from dual;
--(7)创建一个函数,以员工号为参数,返回该员工所在的部门的平均工资。
create or replace function f_sxt7(v_empno emp.empno%type) return emp.sal%type is
vr_sal emp.sal%type;
begin
select avg(sal) into vr_sal from emp where deptno = (select deptno from emp where empno = v_empno);
return vr_sal;
end;
--(7)执行
select f_sxt7(7369) from dual;
--(8)创建一个存储过程,以员工号和部门号作为参数,修改员工所在的部门为所输入的部门号。
--如果修改成功,则显示“员工由……号部门调入调入……号部门”;如果不存在该员工,则显示
--“员工号不存在,请输入正确的员工号。”;如果不存在该部门,则显示
--“该部门不存在,请输入正确的部门号。”。
create or replace procedure p_sxt14(v_empno in emp.empno%type, v_deptno in emp.deptno%type) is
vt_empno number := 0;
vt_deptno number := 0;
vm_deptno emp.deptno%type;
begin
select count(*) into vt_empno from emp where empno = v_empno;
select deptno into vm_deptno from emp where empno = v_empno;
select count(distinct deptno) into vt_deptno from emp where deptno = v_deptno;
if vt_empno = 0 then
dbms_output.put_line('员工号不存在,请输入正确的员工号。');
end if;
if vt_deptno = 0 then
dbms_output.put_line('该部门不存在,请输入正确的部门号。');
end if;
if vt_empno = 1 and vt_deptno = 1 then
dbms_output.put_line('员工由 ' || vm_deptno || ' 号部门调入调入 ' || v_deptno || ' 号部门');
update emp set deptno = v_deptno where empno = v_empno;
commit;
end if;
end;
--(8)执行
begin
p_sxt14(7369,30);
end;
--(9)创建一个存储过程,以一个整数为参数,输入工资最高的前几个(参数值)员工的信息。
create or replace procedure p_sxt15(v_number in number) is
cursor c_emp is select * from emp order by sal desc;
v_n number := 0;
begin
for v_emp in c_emp loop
v_n := v_n + 1;
dbms_output.put_line(v_n || ' - ' || v_emp.ename || ' - ' || v_emp.sal);
if v_n = v_number then
exit;
end if;
end loop;
end;
--(9)执行
begin
p_sxt15(5);
end;
--(10)创建一个存储过程,以两个整数为参数,输出工资排序在两个参数之间的员工信息。
create or replace procedure p_sxt16(v_up in number,v_down in number) is
cursor c_emp is select * from emp order by sal desc;
v_n number := 0;
begin
for v_emp in c_emp loop
v_n := v_n + 1;
if v_n >= v_up and v_n <= v_down then
dbms_output.put_line(v_n || ' - ' || v_emp.ename || ' - ' || v_emp.sal);
end if;
end loop;
end;
--(10)执行
begin
p_sxt16(2,3);
end;
Oracle存储过程及函数的练习题的更多相关文章
- oracle 存储过程,函数和包
创建存储过程: 语法:create [or replace] PROCEDURE 过程名(参数列表) AS PLSQL子程序体: 调用 存储过程的方式 两种1.execute(exec) - ...
- JAVA调用Oracle存储过程和函数
连接数据库的工具类: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; imp ...
- oracle 存储过程,存储函数,包,
http://heisetoufa.iteye.com/blog/366957 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的PL/SQL块.但存储过程和函数不同于已经介绍过 ...
- Oracle存储过程、函数、包加密wrap
wrap加密可以将PL/SQL的代码实现部分隐藏,提高代码的安全性,如存储过程.函数.包等都隐藏. wrap加密的方法有两种,下面以函数为例分别介绍一下: 方法一: 编写好函数后保存到 d:\test ...
- Oracle————存储过程与函数
存储过程存储过程参数模式包括IN.OUT. IN OUT. IN(默认参数模式):表示当存储过程别调用时,实参值被传递给形参:形参起变量作用,只能读该参数,而不能修改该参数.IN模式参数可以是变量或表 ...
- Oracle存储过程及函数
1.在Oracle中,存储过程包括三部分组成:定义部分.执行部分.和异常处理部分(即例外) eg1:输入员工编号,查询员工的姓名和薪资 create or repalce procedure myp ...
- Oracle存储过程和函数使用方法
一.存储过程(PROCEDURE) 使用过程, 不仅可以简化客户端应用程序的开发和维护,而且可以提高应用程序的运行性能. CREATE [OR REPLACE] PROCUDURE procedur ...
- oracle存储过程、函数、序列、包
一. 存储过程 1. 语法 create or replace procedure procedureName(seqName varchar2) is /*声明变量*/ n ); cursor cu ...
- Oracle - 存储过程、函数、包的使用练习-雇员
--存储过程范例:得到雇员表 emp 的记录数 begin --说明:若过程中要向外抛异常,请使用 exception when others then raise; 这个抛出的异常在程序里是可以捕获 ...
随机推荐
- 字节数组byte[]和整型,浮点型数据的转换——Java代码
近期在写C++ socket和java socket之间的通信程序,涉及到整数浮点数的传输.须要从字节数组还原数据,查了一些资料.总结例如以下 1. 整数和浮点数的机器表示 在机器内部.不 ...
- POJ 3221 Diamond Puzzle.
~~~~ 题目链接:http://poj.org/problem? id=3221 显然是BFS找最优解.但是终止条件不好写.看到有一仅仅队交上去一直TLE. 比赛完了看题解原来是以目标状态为起点,B ...
- SmartDispatcher 类
UI线程中使用 public class SmartDispatcher { public static void BeginInvoke(Action action) { if (Deploymen ...
- compileSdkVersion, minSdkVersion 和 targetSdkVersion的选择(copy)
英文原文:Picking your compileSdkVersion, minSdkVersion, and targetSdkVersion 作者:Ian Lake,Google Android ...
- MSXML2.XMLHTTP.4.0对象
一.使用步骤:1.创建XMLHTTP对象 //需MSXML4.0支持2.打开与服务端的连接,同时定义指令发送方式,服务网页(URL)和请求权限等.客户端通过Open命令打开与服务端的服务网页的连接.与 ...
- jQueryTools-Scrollable.js
转载一篇例子,学习使用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...
- 51nod1446 Kirchhoff矩阵+Gauss消元+容斥+折半DFS
思路: //By SiriusRen #include <cstdio> #include <cstring> #include <algorithm> using ...
- 对于民科吧s5_or吧友自增树的复杂度计算
原帖 自增树如s5_or所说,是一种思想像Splay的数据结构,每个节点维护一个堆权值,每当询问一个节点时,堆权值++,并返回时维护堆权值为堆的性质.这个树从旋转次数上比Splay小是肯定的,因为Sp ...
- 涨知识 --- VI
1.空类所占空间大小 空类所占空间为1,单一继承的空类空间也为1,多继承的空类空间还是1.但是虚继承涉及虚表(虚指针),所以sizeof(C)的大小为4. 2.内联函数与宏定义 Ans:内联函数和普通 ...
- NPOI复制模板导出Excel
本人菜鸟实习生一枚,公司给我安排了一个excel导出功能.要求如下:1.导出excel文件有样式要求:2.导出excel包含一个或多个工作表:3.功能做活(我的理解就是excel样式以后可能会变方便维 ...