PLSQL 基础语法(二)

一、条件判断

1.1 需求

不同工资区间有不同的税率,计算税后工资

1.2 语句

ini 复制代码
-- 条件判断
declare 
  tax1 number(10,2);
  tax2 number(10,2);
  tax3 number(10,2); 
  employee employees%rowtype;
  money number(10,2);
begin
  tax1:=1;
  tax2:=0.97;
  tax3:=0.95;
  select * into employee from employees where employee_id = 100 ;   
  -- 计算阶梯工资
  if employee.salary<=5000
    then money:=employee.salary * tax1;
  elsif employee.salary<=10000
    then money:=employee.salary * tax2;
  else
    money:=employee.salary * tax3;
  end if;
    
  DBMS_OUTPUT.put_line('工资:'||employee.salary || ',扣完税后为:'||money);
exception
  when no_data_found then
     DBMS_OUTPUT.put_line('没有找到该用户');
 when too_many_rows then
     DBMS_OUTPUT.put_line('存在多条符合条件的数据');
end;

二、循环

2.1 无条件循环

2.1.1 语法结构

arduino 复制代码
loop
 -- 循环语句
end loop;

2.1.2 范例

输出从1开始的100个数

vbnet 复制代码
declare
  num number :=1;
begin
  loop
    dbms_output.put_line(num);
    num := num+1;
    exit when num > 100;
  end loop;
end;

2.2 条件循环

2.2.1 语法结构

arduino 复制代码
while 条件
loop
    -- 循环语句
end loop;

2.2.2 范例

dart 复制代码
declare
  num number;
begin
  num:=1;  
  while num<=100
  loop
    dbms_output.put_line(num);
    num := num+1;
  end loop;
end;

2.3 for循环

arduino 复制代码
declare
  num number;
begin
  
  for num in 1 .. 100
  loop
    dbms_output.put_line(num);
  end loop;
  
end;

三、游标

3.1 语法结构

声明游标

sql 复制代码
cursor 游标名称 is SQL语句

使用游标

sql 复制代码
open 游标名称
loop
    fetch 游标名称 into 变量
    exit when 游标名称%notfound
end loop;
close 游标名称

3.2 范例

3.2.1 需求

输出 job_id 为 AD_VP 的员工薪资信息

3.2.2 语句

sql 复制代码
declare 
  cursor cur_employee is select * from EMPLOYEES t where job_id = 'AD_VP'; --申明游标
  employee employees%rowtype;
begin
  open cur_employee; --打开游标
  loop
    fetch cur_employee into employee; --提取游标
    exit when cur_employee%notfound; --退出游标标识
    dbms_output.put_line('员工id为' || employee.employee_id || '的薪资为'||employee.salary);
  end loop;
  close cur_employee; -- 关闭游标
end;

3.3 带参数的游标

sql 复制代码
declare 
  cursor cur_employee(type char) is select * from EMPLOYEES t where job_id = type; --申明游标
  employee employees%rowtype;
begin
  open cur_employee('IT_PROG'); --打开游标
  loop
    fetch cur_employee into employee; --提取游标
    exit when cur_employee%notfound; --退出游标标识
    dbms_output.put_line('员工id为' || employee.employee_id || '的薪资为'||employee.salary);
  end loop;
  close cur_employee; -- 关闭游标
end;

3.4 for 循环游标

sql 复制代码
-- for 循环游标
declare 
  cursor cur_employee(type char) is select * from EMPLOYEES t where job_id = type; --申明游标
  employee employees%rowtype;
begin
  for employee in cur_employee('IT_PROG')
  loop
    dbms_output.put_line('员工id为' || employee.employee_id || '的薪资为'||employee.salary);
  end loop;
end;
相关推荐
程序员岳焱6 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
麦兜*6 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
大只鹅7 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
ai小鬼头7 小时前
AIStarter如何快速部署Stable Diffusion?**新手也能轻松上手的AI绘图
前端·后端·github
IT_10247 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
bobz9658 小时前
动态规划
后端
stark张宇8 小时前
VMware 虚拟机装 Linux Centos 7.9 保姆级教程(附资源包)
linux·后端
亚力山大抵9 小时前
实验六-使用PyMySQL数据存储的Flask登录系统-实验七-集成Flask-SocketIO的实时通信系统
后端·python·flask
超级小忍9 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
CHENWENFEIc10 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试