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