一. if/else

语法:
if 条件表达式 then
语句块;
if 条件表达式 then

语句块
end if;
elsif 条件表达式 then
语句块;
...
else
语句块;
end if;
举例:输入一个员工编号,给该员工涨奖金。策略是这样的:
如果原来员工没有奖金,则把基本工资的百分之10作为奖金,如果原来员工的奖金低于1000,把奖金提升到
1000,其他情况奖金提升百分之10。

declare
-- 声明奖金的变量
v_comm emp.comm%type;
begin
-- 查询出员工的奖金
select comm into v_comm from emp where empno=&no;
-- 判断如果员工没有奖金,把基本工资的百分之10作为奖金
if v_comm is null then
update emp set comm=sal*0.1 where empno=&no;
--如果原先奖金低于1000,提升到1000
elsif v_comm<1000 then
update emp set comm=1000 where empno=&no;
-- 其他情况把奖金提升百分之10
else
update emp set comm=comm*1.1 where empno=&no;
end if;

二.case when

case when 和java中switch语句比较像。
java中switch:
switch(变量名){
case 变量值1 :

语句块;
break;
case 变量值2 :
语句块;
break;
.....
default:
语句块;
}
case when 语法:
case 变量名
when 变量值1 then
语句块;
when 变量值2 then
语句块;
........
else:
语句块;
end case;
举例:根据用户输入的部门编号,输出不同的部门名称,要求使用case when实现,不查询dept表

declare
-- 声明部门编号的变量
v_deptno emp.deptno%type:=&no;
begin
case v_deptno
when 10 then
dbms_output.put_line('技术部');
when 20 then
dbms_output.put_line('销售部');
when 30 then
dbms_output.put_line('公关部');
when 40 then
dbms_output.put_line('开发部');
-- 其他情况
else
dbms_output.put_line('您要查找的部门不存在');
end case;
end;

if else 都能把case when 替代。case when 也可以替代if else.
语法:
case
when 条件表达式1 then
语句块;
when 条件表达式2 then
语句块;
....
else
语句块;
end case;
举例:

declare
-- 声明奖金的变量
v_comm emp.comm%type;
begin
-- 查询出员工的奖金
select comm into v_comm from emp where empno=&no;
-- 判断如果员工没有奖金,把基本工资的百分之10作为奖金
case
when v_comm is null then
update emp set comm=sal*0.1 where empno=&no;
--如果原先奖金低于1000,提升到1000
when v_comm<1000 then
update emp set comm=1000 where empno=&no;
-- 其他情况把奖金提升百分之10
else
update emp set comm=comm*1.1 where empno=&no;
end case;
end;

最新文章

  1. 【FTP】FTP文件上传下载-支持断点续传
  2. oracle 常用sql
  3. Uart的Verilog建模
  4. java io操作常规
  5. Python 文件I/O
  6. 64位Ubuntu 13.04 安装Bochs 2.3.5
  7. 星级评论jq
  8. Command-line tools can be 235x faster than your Hadoop cluster
  9. Find the largest multiple of 3 解答
  10. ADO.NET 数据访问类查询、属性扩展
  11. (简单) POJ 3087 Shuffle&#39;m Up,枚举。
  12. 妙用 `package.json` 快速 `import` 文件(夹)
  13. JavaSE:八种基本数据类型
  14. Linux新手随手笔记1.7
  15. 我所知道的JS调试
  16. 201771010134杨其菊《面向对象程序设计java》第七周学习总结
  17. golang学习笔记13 Golang 类型转换整理 go语言string、int、int64、float64、complex 互相转换
  18. [Java]Object有哪些公用方法?
  19. Spring MVC的路径匹配规则 Ant-style
  20. 点击超链接打开本地QQ

热门文章

  1. 【Server】Windows系统安装Tomcat服务器
  2. 如何用python爬虫从爬取一章小说到爬取全站小说
  3. L24 word2vec
  4. 带权值的LCA
  5. mybatis 批量删除
  6. testlink数据表分析
  7. alfred workflow 开发
  8. 二叉树中两节点的最近公共父节点(360的c++一面问题)
  9. 安卓微信浏览器中window.location.href失效的问题
  10. uniqid用法