编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的范围内,否则不予操作,并说明原因

设计思路
1.定义最高工资和最低工资字段变量
2.修改员工号为201号员工的工资。
3.将员工号是201号员工所对应的职位号的最高工资和最低工资into到相应变量
4.若8000元不在范围之内则不予操作并提示。

set serveroutput on
declare sal_min employees.salary%type;sal_max employees.salary%type;
beginselect jobs.max_salary,jobs.min_salary into sal_max,sal_min from jobs,employees where jobs.job_id=employees.job_id and employees.employee_id=201;if 8000 between sal_max and sal_min then update employees set salary=8000 where employee_id=201;else dbms_output.put_line('不在允许的范围之内');end if;
end;
编写一个PL/SQL块,修改员工号为201的员工工资为8000元,保证修改后的工资在职位允许的范围内,否则在修改后取消操作,并说明原因
set serveroutput on;
declaresal_max employees.salary%type;sal_min employees.salary%type;sal employees.salary%type;e exception;
beginselect jobs.max_salary,jobs.min_salary into sal_max,sal_min from jobs,employees where jobs.job_id=employees.job_id and employees.employee_id=201;update employees set salary=8000 where employee_id=201 returning salary into sal;if sal not between sal_max and sal_min thenraise e;end if;exceptionwhen e then dbms_output.put_line('工资不在最低及最高范围内,请重新操作!');rollback;
end;
编写一个PL/SQL块。对所有员工按他们的工资的10%判断。如果大于300就不加薪。否则加10%。同时 给与提示
set serveroutput on;
declarecursor e_sal is select * from employees;sal employees.salary%type;empid employees. employee_id %type;
beginfor employee in e_sal loopsal :=employee.salary*0.1;if sal>300 thendbms_output.put_line('不可以加薪');elsedbms_output.put_line('可以加薪');empid :=employee.employee_id;dbms_output.put_line(empid);update employees set salary=salary+sal where employee_id=empid; end if;end loop;
end;
编写一个PL/SQL块,输出所有员工的员工姓名、员工号、工资和部门号
set serveroutput on;
begin
for v_emp in  (select employee_id, last_name  from employees ) loop
dbms_output.put_line(v_emp.employee_id||''||v_emp.last_name);
end loop;
end; 
编写一个PL/SQL块,输出所有比本部门平均工资高的员工信息
set serveroutput on;
declarev_avgsal employees.salary%type;
beginfor v_emp in (select * from employees) loopselect avg(salary) into v_avgsal from employeeswhere department_id=v_emp.department_id;
if v_emp.salary>v_avgsal thendbms_output.put_line(v_emp.first_name||' '||v_emp.last_name||' '||v_emp.employee_id||' '||v_emp.salary||' '||v_emp.department_id);end if;   end loop;
end;
编写一个PL/SQL块,输出所有员工及其部门领导的姓名、员工号及其部门号
set serveroutput on;
declarecursor c_emp is
select e.employee_id eid,e.last_name ename,e.department_id edid,
m.employee_id mid,m.last_name mname
from employees e,employees m
where e.manager_id=m.employee_id;
beginfor v_emp in c_emp loopdbms_output.put_line(v_emp.eid||' '||v_emp.ename||' '||v_emp.edid||' '||v_emp.mid||' '||v_emp.mname);end loop;
end;
查询last_name 是Smith的员工工资,如果该员工不存在,则输出“There is not such an employee!”;如果存在多个同名的员工,提示该姓有多名员工,同时输出其员工号和工资
set serveroutput on;
declarev_emp employees%rowtype;
beginselect * into v_emp from employees where last_name = 'Smith';dbms_output.put_line(v_emp.employee_id||''||v_emp.first_name||''|| v_emp.last_name||' '||v_emp.salary);
exceptionwhen no_data_found thendbms_output.put_line('There is not such an employee!');when too_many_rows thenfor v_emp in (select * from employees where last_name = 'Smith') loopdbms_output.put_line(v_emp.employee_id||' '||v_emp.first_name||' '|| v_emp.last_name||' '||v_emp.salary);end loop;
end;
用用户定义异常方法设计修改108员工的工资,保证修改后工资不超过6000
set serveroutput on;
DECLAREe_highlimit EXCEPTION;v_sal employees.salary%TYPE;
BEGINUPDATE employees SET salary=salary+100 WHERE employee_id=108 RETURNING salary INTO v_sal;IF v_sal>6000 THEN RAISE e_highlimit;END IF;
EXCEPTIONWHEN e_highlimit THENDBMS_OUTPUT.PUT_LINE('The salary is too large! ');ROLLBACK;
END;

不用异常方式

DECLAREe_highlimit EXCEPTION;v_sal employees.salary%TYPE;
BEGINUPDATE employees SET salary=salary+100 WHERE employee_id=108 RETURNING salary INTO v_sal;IF v_sal>6000 THEN DBMS_OUTPUT.PUT_LINE('The salary is too large! ');ROLLBACK;END IF;
END;
用SELECT INTO 语句输出last_name是’Smith’的工资,用OTHERS异常处理器及用SQLCODE:返回当前错误代码,用SQLERRM:返回当前错误的消息文本
DECLAREv_sal employees.salary%TYPE;v_code NUMBER(6);v_text VARCHAR2(200);
BEGINSELECT salary INTO v_sal FROM employees WHERE last_name='Smith';DBMS_OUTPUT.PUT_LINE('salary: '||v_sal);
EXCEPTIONWHEN OTHERS THENv_code:=SQLCODE;v_text:=SQLERRM;   DBMS_OUTPUT.PUT_LINE(v_code||' '||v_text);
END;
创建一个存储过程,以部门号为参数输出该部门的人数,平均工资(保留两位小数),最高工资、最低工资
create or replace
procedure proc_deptinfo(p_deptno employees.department_id%type
)
as
p_count NUMBER;
p_avgsal employees.salary%type;
p_maxsal employees.salary%type;
p_minsal employees.salary%type;
beginselect count(*),avg(salary),max(salary), min(salary) into p_count,p_avgsal, p_maxsal, p_minsal from employees where department_id=p_deptno;dbms_output.put_line('部门号是:'||p_deptno||' 的部门的人数是:'||round(p_count,2)||' 部门的平均工资是:'||p_avgsal||' 最高工资是:'|| p_maxsal||' 最低工资是:'|| p_minsal);
exception when no_data_found then dbms_output.put_line('The department don''t exists!');
end proc_deptinfo;-- client
set serveroutput on;
declare p_deptno employees.department_id%type;
beginp_deptno := &x;proc_deptinfo(p_deptno);
end;
创建一个函数,以部门编号为参数,返回部门的平均工资
create or replace function f_getavgsal(f_dno employees.department_id%type)
return number--返回工资的数据类型
as
f_avgsal employees.salary%type;
begin
select avg(salary) into f_avgsal from employees where department_id= f_dno;
return f_avgsal;--返回工资
end f_getavgsal;--client
set serveroutput on;
declare p_deptno employees.department_id%type;v_avgsal employees.salary%type;
beginp_deptno := &x;v_avgsal := f_getavgsal(p_deptno);DBMS_OUTPUT.PUT_LINE('部门号是:'||p_deptno||' 的部门平均工资是:'||v_avgsal);
end;
编写PL/SQL块,根据输入的员工号将该员工的工资增加600(禁止在每天8:00-12:00之外的时间进行增加、修改、删除操作)
create or replace
trigger trg_emp
before insert or update or delete
on employees
beginif to_char(sysdate, 'hh24:mi') not between '08:00' and '12:00' then raise_application_error(-20005,'只能在上班时间修改');end if;
end trg_emp;--client
set serveroutput on;
declaree exception;pragma exception_init(e,-20005);employeeId employees.employee_id%type;
beginemployeeId := &x;update employees set salary= salary+600 where employee_id=employeeId;dbms_output.put_line('修改成功!');exception
when e then
dbms_output.put_line('只能在上班时间修改');
end;

PL/SQL基础题型相关推荐

  1. Oracle PL/SQL基础知识

    Oracle PL/SQL基础知识 过程(存储过程) 过程用于执行特定的操作,当建立过程时,既可以指定输入参数(in),也可以指定输出参数(out).通过在过程中使用输入参数,可以将数据传递到执行部分 ...

  2. ORACLE甚而,PL/SQL基础

    Oracle PL/SQL语言基础 PL/SQL是ORACLE对标准数据库语言的扩展,ORACLE公司已经将PL/SQL整合到ORACLE 服务器和其他工具中了,近几年中更多的开发人员和DBA开始使用 ...

  3. oracle空间数据库实验报告,Oracle数据库实验报告六 PL/SQL基础

    Oracle数据库实验报告六 PL/SQL基础 -by QQC from BTBU [实验目的] PL/SQL的安装网上有很多教程这里就不做赘述了,如果后序需求大的话我再考虑做一期PL/SQL安装使用 ...

  4. PL/SQL基础之DECLARE部分(整理)

    整理于 Oracle PL/SQL编程详解 - 古立 - 博客园 PL/SQL基础之DECLARE部分整理 PL/SQL块的结构 声明规范 标识符 数据类型 定义记录类型语法: 定义VARRY数据类型 ...

  5. Oracle PL/SQL基础语法学习13:比较运算符

    系列文章目录 Oracle PL/SQL基础语法学习12:短路求值 Oracle PL/SQL基础语法学习13:比较运算符 Oracle PL/SQL基础语法学习14:BOOLEAN表达式 文章目录 ...

  6. Oracle442个应用场景---------PL/SQL基础

    ----------------------------------------------------------------------------------- 备份和恢复数据库略过.在后面解说 ...

  7. pl/sql基础练习

    pl/sql块: 1 匿名块            ----不能存储,不能共享 2 存储过程 函数 触发器 包   ----带有名称的块,可以存储在oracle服务器上,可以共享和多次调用. 1 匿名 ...

  8. 20个案例掌握PL/SQL 基础

    有MS SQL基础,学习了两周多的PL/SQL,做了一些事例,但是很多信息在网上难以找到太多正确的答案,看到一篇又一篇的PL/SQL博文,案例方面的博文一篇又一篇的雷同,一看就是是Ctrl+C的复制. ...

  9. pl/sql基础知识—定义并使用变量

    n  介绍 在编写pl/sql程序是,可以定义变量和常量:在pl/sql程序中包括有: ①标量类型(scalar) ②复合类型(composite) ③参照类型(reference) ④lob(lar ...

  10. SQL基础题型(内含数据库和答案),帮你更加熟练运用SQL

    下面分享关于SQL的题目,包括数据库和答案解析.希望对大家有帮助. 这属于基础题型,大家可以用于日常练习,虽然是基础题,但里面所以知识点都概括了.如果你能够全部理解透彻,真的,这你自己本身是能够感受到 ...

最新文章

  1. word文档怎么给数字加千分符_Word中如何将文档中的金额数值设置为财务数字中的千分位格式...
  2. c语言程序设计01,c语言程序设计01.doc
  3. 数开头的成语有哪些_艺术留学文书申请过程中应避开哪些雷区?ACG艺术留学
  4. 猿团专访云信CTO阙杭宁——网易云信“稳定”背后的秘密
  5. 项目管理---SVN,Subversion的安装,客户端和服务端
  6. 【翻译】如何在Ext JS 6中使用Fashion美化应用程序
  7. .net 创建属于自己的log类
  8. Java学习笔记--反射API
  9. 修改linux文本模式下的分辨率
  10. JSP内置对象session和application
  11. python图书库存管理系统_基于Odoo的物流库存管理系统的设计(Python)
  12. FireBase Android版本测试
  13. CentOs7 中安装 guetzli 详细教程
  14. 返回链表的中间结点,若中间有两个结点,则返回后一个结点(两种方法)
  15. 互联网/移动互联网小团队创业
  16. android tf卡及u盘_一体化储存芯片(U 盘、SD卡、TF卡) NAND定义开发-U盘篇
  17. 混合颜色的色值计算公式
  18. java 统计阅读量_使用redis实现【统计文章阅读量】及【最热文章】功能
  19. python strftime时分秒_Python time.strftime()用法及代碼示例
  20. 数据结构与算法(基于<algorithm>)

热门文章

  1. 51单片机电子琴设计
  2. 幻想破灭!为何“每个儿童一台笔记本电脑”项目屡屡陷入困境?
  3. 远程控制计算机开关机
  4. 【rac asm扩容】在vmware workstation中配置集群(rac)的共享存储(磁盘)方法(流程)
  5. python 饼图笔记 两个饼图 双饼图 复合饼图 两个饼图 环形图
  6. 衣服的尺寸S、M、L、XL、XXL分别表示的型号大小顺序是什么?
  7. matplotlib之pyplot模块——阶梯图(step():基本功能、参数)
  8. 32形容词/副词的原级比较
  9. WebRTC 概念介绍--一篇读懂source、track、sink、mediastream
  10. 抑郁症患者自述:从那天起,我走进了地狱