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;
相关推荐
该用户已不存在14 小时前
macOS是开发的终极进化版吗?
前端·后端
计算机毕业设计木哥14 小时前
计算机毕设选题:基于Python+Django的B站数据分析系统的设计与实现【源码+文档+调试】
java·开发语言·后端·python·spark·django·课程设计
歪歪10014 小时前
qt creator新手入门以及结合sql server数据库开发
c语言·开发语言·后端·qt·数据库开发
布列瑟农的星空14 小时前
大话设计模式——观察者模式和发布/订阅模式的区别
前端·后端·架构
Moonbit14 小时前
月报Vol.03: 新增Bitstring pattern支持,构造器模式匹配增强
后端·算法·github
中等生15 小时前
Pandas 与 NumPy:数据分析中的黄金搭档
后端·python
用户83562907805115 小时前
Python查找替换PDF文字:告别手动,拥抱自动化
后端·python
小猪乔治爱打球15 小时前
[Golang 修仙之路] 分布式专题:分布式锁
后端·面试
似水流年流不尽思念15 小时前
LBCC和MVCC的区别和优缺点
后端
愿你天黑有灯下雨有伞15 小时前
一种基于注解与AOP的Spring Boot接口限流防刷方案
java·spring boot·后端