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; 这个抛出的异常在程序里是可以捕获 ...
随机推荐
- scikit-learn:matplotlib.pyplot经常使用绘图功能总结(1)
參考:http://matplotlib.org/api/pyplot_api.html 绘图功能总结(2):http://blog.csdn.net/mmc2015/article/details/ ...
- Unity游戏小地图生成
孙广东 2015.6.25 这个在AssetStore上有几个不错的插件, 除了知道原理,能自己实现还是好的. 非常多插件是不会直接使用的.而是要依据自己项目的需求进行改动或者就是自己写. 那么我们 ...
- 修改this指向(bind、call 和 apply)
一.bind 首先: var alertWrite = document.write; alertWrite('who am I?'); 这两行代码的运行结果是什么呢?不要急着回答,看完下面的内容再回 ...
- Java知识总结---整合SpringMVC+Mybatis+Spring(二)
在如今的Java Web开发过程中,各种各样框架层出不穷.在工作中,框架的使用也越来越频繁. 今天介绍一下如今比較流行的SpringMVC.Mybatis和Spring框架.学习一下怎样在项目中使用它 ...
- uefi bios安装ubuntu16.04 (win10和ubuntu双系统)
哎呀,没事闲的装双系统,按照晚上的教程装半天也没成功,后来才知道是自己电脑的问题,当然也有那些过时的博客的问题! ultraiso制作ubuntu u盘启动盘 http://www.cr173.co ...
- [模板] manacher(教程)
还是不会马拉车啊.今天又学了一遍,在这里讲一下. 其实就是一个很妙的思路,就是设置一个辅助的数组len,记录每个点的最大对称长度,然后再存一个mx记录最大的对称子串的右端点.先开二倍数组,然后一点点扩 ...
- spring web中的filter
昨天看了会spring web中部分代码,主要是各种filter,回顾一下: Spring的web包中中有很多过滤器,这些过滤器位于org.springframework.web.filter并且理所 ...
- linux下 yum相关
1.1 什么是yum源 Yellowdog Updater, Modified 一个基于RPM包管理的字符前端软件包管理器. 能够从指定的服务器自动下载RPM包并且安装,可以处理依赖性关系,并且一次安 ...
- j建立一个小的servlet小程序
我们建立一个最简单的servlet程序,这个servelt程序只是单纯的输出helloworld. 步骤如下:如图:在Eclipse中选择新建一个项目,其中选择tomcat project然后点击下一 ...
- Flask小总结+实例化Flask参数以及对app的配置
Flask 小而精 三方组件全 稳定性相对较差 1.启动: from flask import Flask app = Flask(__name__) app.run("0.0.0.0&qu ...