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; 这个抛出的异常在程序里是可以捕获 ...
随机推荐
- UVA - 10691 Subway
题目大意:给定n个点.要求建造尽量少得铁路(从原点发射出的射线).使得全部点到铁路的最短距离小于d. 解题思路:题目能够转化成区间选点问题,即以极角来表示铁轨.然后计算出每一个区间可行的极角范围,进行 ...
- centos6.4 ssh免密码登陆(只需三个步骤)
学习Hadoop的时候,用到的.这里作为记录. 以下是最简洁的方式: 4台虚拟机: 用户:root.hadoop hostname 分别是:Master.Hadoop.Slave1.Hadoop.Sl ...
- cloudstack ---部署的架构
cloudstack跟KVM一起部署的架构 下图是CloudStack跟kvm一起部署的架构: 在每个kvm的宿主机上都需要部署agent程序. cloudstack跟vsphere一起部署的架构 下 ...
- php打包文件夹成zip文件
function addFileToZip($path,$zip){ $handler=opendir($path); //打开当前文件夹由$path指定. while(($filenam ...
- TextMeshPro 图片字Sprite
生成 需要一个资源 右键生成 调整位置 放在目录 使用 <sprite="NumDamage" index=1><sprite="NumDam ...
- JavaScript(JS)的简单使用
一.什么是JS(Javascript)? Javascript是一种脚本语言,被广泛用于Web应用开发,常用来为网页添加各式各样的功能,为用户提供更加流畅的浏览效果. Javascript严格区分大小 ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- 【Leetcode 86】 Partition List
问题描述: 给定一个list, 将所有小于x的node放到左边,剩下的保持原样. 问题解决: 闲的无聊,用c++和python都做了一遍. 代码如下: # Definition for singly- ...
- (转)Vue 爬坑之路(四)—— 与 Vuex 的第一次接触
在 Vue.js 的项目中,如果项目结构简单, 父子组件之间的数据传递可以使用 props 或者 $emit 等方式 http://www.cnblogs.com/wisewrong/p/62660 ...
- Server Tomcat v8.0 Server at localhost failed to start 问题解决方法?
bi编程jsp servlet 第一个程序: HelloServlet 运行错误 404: 十月 28, 2017 11:25:14 上午 org.apache.tomcat.util.digest ...