if-else运用

declare
v_num number(8);
begin
v_num :=&n;
if v_num>0 and v_num<100 then
dbms_output.put_line('工薪阶级');
elsif v_num between 100 and 200 then
dbms_output.put_line('中产阶级');
else
dbms_output.put_line('资本家');
end if;
end;

case运用

declare
v_num number(8) :=&n;
v_result varchar2(15);
begin
case
when v_num =1 then v_result :='剪刀';
when v_num =2 then v_result :='石头';
when v_num =3 then v_result :='布';
else
v_result :='输入的数字无效';
end case;
dbms_output.put_line(v_result);
end;

--案例:1-10输出(loop循环实现)

declare
i number(8) :=1;
begin
loop
dbms_output.put_line(i);
exit when i =10;
--改变初始值
i := i+1;
end loop;
end;
/

--while循环实现上面例子

declare
i number(8) :=1;
begin
while i<=10 loop
dbms_output.put_line(i);
--改变初始值
i := i+1;
end loop;
end;
/

--for循环输出1-10

结构特点

for i in() loop
end loop;
declare
--i number(8) :=&n;
begin
for i in 1..10 loop --初始值..结束值
dbms_output.put_line();
end loop;
end;
/

for循环反向输出 10-1

declare
--i number(8) :=&n;
begin
for i in resever 1..10 loop --初始值..结束值
dbms_output.put_line(i);
end loop;
end;
/
declare
v_a number(8);
begin
v_a :=1;
if v_a= 1 then
dbms_output.put
declare
num number(8) :=&n;
result number(8);
--f(n) number(8);
begin
case
when num=1 then result :=1;
when num=2 then result =result

作业if-else 结构

1、输入员工的编号,判断此员工的工资:

-如果工资sal<1000则让工资在原来在基础上加上100

-如果工资1000<= sal <2000则让工资在原来在基础上加上200

-否则让员工工资在原来的基础上加上300;

declare
v_empno number(30) :=&n;
v_sal number(30);
begin
select sal into v_sal from emp where empno=v_empno;
if v_sal<1000 then v_sal :=v_sal +100;
elsif v_sal between 1000 and 2000 then v_sal:=v_sal+200;
else
v_sal :=v_sal+300;
end if;
dbms_output.put_line(v_sal);
end;
/

2.编写一个过程,可以输入一个雇员名,如果该雇员的工资低于2000,就给该员工工资增加10%。(使用if)

declare
v_ename varchar2(30) :=&n;
v_sal number(30);
begin
select sal into v_sal from emp where ename=v_ename;
if v_sal<2000 then v_sal :=v_sal+(v_sal*0.1);
end if;
dbms_output.put_line(v_sal);
end;
/

3.编写一个过程,可以输入一个雇员名,如果该雇员的补助不是0 就在原来的基础上增加100;如果补助为0 就把补助设为200;(使用if...else)

declare
v_ename varchar2(20) :=&n;
--v_money number(30);
v_nvl number(20);
begin
select nvl(comm,0) into v_nvl from emp where ename=v_ename;
if(v_nvl =0) then v_nvl :=v_nvl+200;
else v_nvl := v_nvl+100;
end if;
dbms_output.put_line(v_nvl);
end;
/

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

--如果该雇员的职位是MANAGER 就给他的工资增加500,其它职位的雇员工资增加200。(使用if..elsif..else)

declare
v_empno number(32) :=&n;
v_job varchar2(20);
v_sal number(20);
begin
select job,sal into v_job,v_sal from emp where empno=v_empno;
if(v_job ='PRESIDENT') then v_sal :=v_sal+1000;
elsif (v_job = 'MANAGER') then v_sal :=v_sal+500;
else v_sal :=v_sal+200;
end if;
dbms_output.put_line(v_sal);
end;
/

作业循环结构

1、循环输出“haha1...haha10”(使用while)

declare
i number(5) :=1;
begin
while i<10 loop
dbms_output.put_line('haha' || i);
i :=i+1;
end loop;
end;
/

2、把上述示例改为loop实现

declare
i number(5) :=1;
begin
loop
dbms_output.put_line('haha' || i);
exit when i =10;
i :=i+1;
end loop;
end;
/

3、现有一张表users,字段(uid,uname),分别使用(loop、while、for完成)。

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

create table users
(
userid number(8),
uname varchar2(20)
); declare
v_id number(8):=1;
v_name varchar2(20);
begin
while v_id<=3 loop
v_name:='&name';
insert into users values(v_id,v_name);
v_id:=v_id+1;
end loop;
end;
/

4、打印九九乘法表

这个还要修改

declare
v_result number(20);
--i number(20) :=1;
begin
for i in 1..9 loop;
for j in 1..9 loop;
v_result :=i*j;
if length(i*j)=1 and j!=1 then
dbms_output.put_line(' ');
end if;
dbms_output.put_line(i || '*'||j ||'='||v_result);
dbms_output.put_line(' ');
end loop;
end;

这是对的

declare
begin
for i in 1..9 loop
for j in 1..i loop
dbms_output.put(i);
dbms_output.put('*');
dbms_output.put(j);
dbms_output.put('=');
if length(i*j)=1 and j!=1 then
dbms_output.put(' ');
end if;
dbms_output.put(i*j);
dbms_output.put(' ');
end loop;
dbms_output.put_line(' ');
end loop;
end;

最新文章

  1. setAttribute()
  2. using关键字背后的故事!
  3. redis在window环境下的安装
  4. ROS2.9.27架设网吧软路由实战篇之端口映射与回流
  5. leetcode:Reverse Bits
  6. [Effective C++ --009]确定对象被使用前已先被初始化
  7. POJ 3352 Road Construction (边双连通分量)
  8. iostat详解
  9. Hibernate(八)一对多单向关联映射
  10. JIRA官方:JIRA定制工作流
  11. 使用nfs作为根文件系统启动,(3)
  12. (NO.00002)iOS游戏精灵战争雏形(八)
  13. Spring Framework学习要点摘抄
  14. C#一句话判断两个List&lt;T&gt;是否相等
  15. EF Code First列名 &#39;Discriminator&#39; 无效的问题
  16. 51.webpack vue-cli创建项目
  17. 第一个React Native程序踩到的那些坑
  18. 自学Linux Shell13.2-选项处理(主要getopt、getopts命令)
  19. Gravitee.io Access Management docker-compose运行
  20. Elastalert 监控

热门文章

  1. CF70D Professor&#39;s task(动态凸包)
  2. MVVM与Controller瘦身实践
  3. 避免picture图片无法删除,提示正在被其他进程使用
  4. Python的自增运算与Python变量的浅析
  5. 基于rabbitMQ 消息延时队列方案 模拟电商超时未支付订单处理场景
  6. 用js实现倒计时功能
  7. Idea提示没有符号类错误解决
  8. 关于Sumlime和其插件的安装
  9. 操作iframe的一些方法
  10. CollectionUtils.select 集合筛选