当前位置: 首页 > news >正文

PL/SQL编程(四)

七、pl/sql基础语法—条件循环等

1. if条件

1)if—then

--编写一个过程,输入雇员名,如果该雇员工资低于2000,就增加10%

create or replace procedure test_pro(name varchar2) is

--定义部分

v_sal emp.sal%type;

begin

--执行部分

select sal into v_sal from emp where ename=name;

if v_sal<2000 then

update emp set sal=sal+sal*10% where ename=name;

end if;

end;

 

2)if—then—else

--编写一个过程,输入雇员名,如果该雇员的补助不是0就在原来的基础上增加100;如果补助为0就把补助设为200

create or replace procedure test_pro(name varchar2) is

--定义部分

v_comm emp.comm%type;

begin

select comm into v_comm from emp where ename=name;

if v_comm <> 0 then

update emp set comm=comm+100 where ename=name;;

else

update emp set comm=200 where ename=name;

end if;

end;

 

3)if—then—elsif—then

--编写一个过程,输入雇员编号,如果该雇员的职位是PRESIDENT就给他工资增加1000;如果该雇员的职位是MANAGER工资就增加500

--其他职位的雇员员工工资增加200

create or replace procedure test_pro(no number) is

--定义部分

v_job emp.job%type;

begin

--执行部分

select job into v_job from emp where empno=no;

if v_job='PRESIDENT' then

update emp set sal=sal+1000 where empno=no;

elsif v_job='MANAGER' then

update emp set sal=sal+500 where empno=no;

else

update emp set sal=sal+200 where empno=no;

end if;

end;

 

 

2. 循环—loop

1)loop是pl/sql中最简单的循环,以loop开头,以end loop结尾。这种循环至少会被执行一次。

--案例

--现有一张users表,结构为用户id和用户名2个字段组成

--请编写一个过程,可以输入用户名,并循环添加用户到users表中,用户编号从1开始

create or replace procedure test_pro(userName varchar2) is

--定义一个索引

v_index_num number :=1;

begin

loop

insert into users(userId,userName) values(v_index_num,userName);

--判断是否要退出循环

exit when v_index_num>=10;

--自增

v_index_num=v_index_num+1;

end loop;

end;

 

2)while循环

loop循环至少要执行一次,而对于while循环来说,只有条件为true时,才会执行循环体语句,while循环以while…loop开始,以end loop结束。

--案例

--现有users表,结构同上。请编写一个过程,输入用户名,并循环添加10个用户到users表中

--用户编号从11开始

create or replace procedure test_pro(userName varchar2) is

--定义变量

v_index_num number:=11;

begin

--循环

while v_index_num>=20 loop

--插入用户名

insert into users(userId,userName) values(v_index_num,userName);

--自增

v_index_num=v_index_num+1

end loop;

end;

 

3)for循环

for循环的基本结构如下:

begin

for i in reverse 1..10 loop

insert into users values(i,'test');

end loop;

end;

其中,控制变量i在隐含中有个自增1的过程。缺点,不灵活。

 

3. 顺序控制语句

1)goto语句

goto语句用于跳到特定的标号去执行语句。

注意:乱用goto语句会增加程序的复杂性,并使得应用程序可读性变差,所以在做一般应用程序开发时,建议大家不要使用goto语句。

基本语法如下,goto lable,其中lable是已经定义好的标号。

declare

i number:=1;

begin

loop

dbms_output.putline('输出i='||i);

if i=10 then

goto end_loop;

end if;

i:=i+1;

end loop;

<<end_loop>>

dbms_output.putline('循环结束');

end;

 

2)null语句

null语句不会执行任何操作,并且会直接将控制传递到下一条语句。

使用null语句的主要好处是可以提高pl/sql的可读性。

declare

v_sal emp.sal%type;

v_ename emp.ename%type;

begin

select enmae,sal into v_ename,v_sal from emp where empno=&no;

if v_sal < 3000 then

update emp set sal=sal*1.1 where ename=v_ename;

else

null;

end if;

end;

 

相关文章:

  • Controller的激活与URL路由
  • 关于网站二级联动菜单前台不能正常显示的问题
  • composer 安装 ubuntu 12.04
  • 在Struts2的Action中取得请求参数值的几种方法 .
  • mysqlreport指南
  • telerik的RadAutoCompleteBox控件学习二
  • (一) storm的集群安装与配置
  • (转载)利用webkit抓取动态网页和链接
  • Silverlight数据绑定引擎
  • 拆箱陷阱
  • 带你攀顶云端高级认证,有这回事?
  • Tomcat7 安装使用及jvm连接数参数调优
  • 开发可统计单词个数的Android驱动程序(3)
  • 使用Vitamio打造自己的Android万能播放器(12)—— 播放网络视频缓冲处理
  • 调试工具-gprof
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • 〔开发系列〕一次关于小程序开发的深度总结
  • Create React App 使用
  • express + mock 让前后台并行开发
  • gf框架之分页模块(五) - 自定义分页
  • HTTP中GET与POST的区别 99%的错误认识
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • JAVA_NIO系列——Channel和Buffer详解
  • Java编程基础24——递归练习
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • linux学习笔记
  • markdown编辑器简评
  • Mithril.js 入门介绍
  • 成为一名优秀的Developer的书单
  • 对JS继承的一点思考
  • 关于Flux,Vuex,Redux的思考
  • 聊聊redis的数据结构的应用
  • 前端js -- this指向总结。
  • 如何使用 JavaScript 解析 URL
  • 如何学习JavaEE,项目又该如何做?
  • 3月7日云栖精选夜读 | RSA 2019安全大会:企业资产管理成行业新风向标,云上安全占绝对优势 ...
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 策略 : 一文教你成为人工智能(AI)领域专家
  • ​ 全球云科技基础设施:亚马逊云科技的海外服务器网络如何演进
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​ubuntu下安装kvm虚拟机
  • ​一些不规范的GTID使用场景
  • # Apache SeaTunnel 究竟是什么?
  • #162 (Div. 2)
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • (C#)Windows Shell 外壳编程系列4 - 上下文菜单(iContextMenu)(二)嵌入菜单和执行命令...
  • (附源码)php投票系统 毕业设计 121500
  • (附源码)ssm失物招领系统 毕业设计 182317
  • (附源码)ssm学生管理系统 毕业设计 141543
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (数位dp) 算法竞赛入门到进阶 书本题集
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (译)计算距离、方位和更多经纬度之间的点
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • .bat批处理(五):遍历指定目录下资源文件并更新