java基础

PL/sql

declare
--列类型
name student.name%type;
--行类型
student_row student%rowtype;
begin
  --查询一行一列数据复制给变量name
  select s.name into name from student s where s.id=1;
  --查询行数据赋值给变量student_row,不能查询到多行,否则报错
  select s.* into student_row from student s where s.id=2;
  dbms_output.put_line('姓名:'||name);
  dbms_output.put_line('编号:'||student_row.id||',姓名:'||student_row.name||',生日:'||student_row.birthday||',创建时间:'||student_row.create_time);
  end;
  
  
  declare
  --声明复合类型
  type student_record is record(
      id student.id%type,
      name student.name%type,
      birthday student.birthday%type,
      create_time student.create_time%type
  );
  --声明复合类型变量student_row
  student_row student_record;
  begin
    --查询字段值赋值给复合类型变量
    select s.id,s.name,s.birthday,s.birthday into student_row from student s where s.id=1;
    dbms_output.put_line('编号:'||student_row.id||',姓名:'||student_row.name||',生日:'||student_row.birthday||',创建时间:'||student_row.create_time);
   end;
   
--输入一个成绩,输出成绩等级
declare
score number;

begin
  score := &请输入成绩;
  if score >=90 then
    dbms_output.put_line('优秀');
     elsif score >=80 then
    dbms_output.put_line('良好');
    elsif score>=70 then
    dbms_output.put_line('中等');
     elsif score>60 then
    dbms_output.put_line('及格');
    else
     dbms_output.put_line('不及格');
     end if;
  end;
--输入成绩等级,输出对应的内容
declare
score varchar2(20) :='&输入成绩等级(A,B,C,D,E)';

begin
  case score
    when 'A' then
     dbms_output.put_line('优秀');
    when 'B' then
      dbms_output.put_line('良好');
    when 'c' then
      dbms_output.put_line('中等');
    when 'D' then
      dbms_output.put_line('及格');
    when 'E' then
      dbms_output.put_line('不及格');
    else
      dbms_output.put_line('输入错误');
    end case;
end;  
  
--loop循环计算1~100的和
declare
  he number :=0;
  i number :=1;

begin
  loop
    he := he + i;
    exit when i = 100;
    i := i + 1;
   end loop;
    dbms_output.put_line('总和:'||he);
  end;
 

--while循环计算1~100的和
declare
  he number :=0;
  i number :=1;

begin
    while i<=100
      loop
        he := he + i;
        i := i+1;
      end loop;
    dbms_output.put_line('总和:'||he);
  end;

--while计算1~100的奇数和、偶数和
declare
  jshe number := 0;
  oshe number :=0;
  i number := 1;
  j number :=2;

begin
  while i<100
  loop
   jshe := jshe+i;
   i := i+2;
   oshe := oshe+j;
   j := j+2;
     
  end loop;
  dbms_output.put_line('奇数和:'||jshe||',偶数和:'||oshe);

  end;
  
 --while计算1~100的奇数和、偶数和
 --方式二:
  declare
  i number := 1;
  ji number := 0;
  ou number := 0;
  
  begin
    while i<100
    loop
      if mod(i,2)=0 then
        ou := ou+i;
        else
        ji := ji+i;
      end if;
      i := i+1;
      
    end loop;
    
    dbms_output.put_line('奇数和:'||ji||',偶数和:'||ou);
    
    end;
    
--for 循环计算1~100之和
declare
he number :=0;
begin
  --i的取值1`100
  for i in reverse 1..100
  loop
     dbms_output.put_line('i:'||i)
     he := he +i;
  end loop;
   dbms_output.put_line('总和:'||he);
  end;
  
--异常
declare
i number := 10;
begin
  dbms_output.put_line('i/0:'||(i/0));
  exception 
  --根据不同的异常处理类型进行处理,ZERO_DIVIDE 除数为0的异常处理
    when ZERO_DIVIDE then
    dbms_output.put_line('除数不能为零');  
    when others then
    dbms_output.put_line('除数不能为零');  

  end;
  
--用户自定义异常
declare
age_exception exception;
sex_exception exception;
sex varchar2(10);
age number;
begin
  sex := '&请输入性别:';
  age := &请输入年龄;
  if sex not in('男','女') then
    --抛出异常
    raise sex_exception;
   
  end if;
  if age not between 0 and 150 then
    raise age_exception;
  end if;
  exception
    --铺获自定义异常
    when sex_exception then
    dbms_output.put_line('性别只能输入男或女');  
    when age_exception then
    dbms_output.put_line('年龄只能输入0~150之间的数');  
  end;



declare
age_exception exception;
sex_exception exception;
sex varchar2(10);
age number;
begin
  sex := '&请输入性别:';
  age := &请输入年龄;
  if sex not in('男','女') then
    --抛出异常
    raise sex_exception;
   
  end if;
  if age not between 0 and 150 then
    raise age_exception;
  end if;
  exception
    when sex_exception then
    --dbms_output.put_line('性别只能输入男或女'); 
    --弹出自定义错误消息窗口,第一参数是错误代码,取值范围是-20001-2099,
    --第二个参数是错误信息描述,最大长度为2048个字符
    raise_application_error(-20001,'性别只能输入男或女') 
    when age_exception then
    --dbms_output.put_line('年龄只能输入0~150之间的数'); 
    raise_application_error(-20002,'年龄只能输入0~150之间的数') 
  end;
  
  
  
--练习:给员工加工资,输入员工编号和要加工资的工资数,如果员工不存在,输出员工不存在
declare
id employees.employee_id%type;
addsalary number;
id_exception exception;

begin
   select e.id into id from employees e where e.id=1;
  id := &请输入员工编号;
  
  if
    
  end if;
  if id not in(e.employee_id) 
    raise id_exception;
  end if;
  exception
    when id_exception then
    dbms_output.put_line('员工不存在')
  end;

--练习:给员工加工资,输入员工编号和要加工资的工资数,如果员工不存在,输出员工不存在

declare
emp_no employees.employee_id%type := &员工编号;
sal employees.salary%type := &新加工资;
begin
  --给变量赋值,如果查询不到值报no_data_found 异常,如果查询到多条数据报too_many_rows 异常
  select e.employee_id into emp_no from employees e where e.employee_id =emp_no;
  update employees e set e.salary = e.salary+sal where e.employee_id = emp_no;
  commit;--提交事务
exception
  when no_data_found then
    dbms_output.put_line('员工不存在');
  end;
  
  
  
select e.* from employees e where e.employee_id=105;


--练习:给员工加工资,输入员工编号和要加工资的工资数,如果员工不存在,输出员工不存在,要求工龄超过十五年
declare
emp_no employees.employee_id%type := &员工编号;
sal employees.salary%type := &新加工资;
hire_time employees.hire_date%type;
begin
  --给变量赋值,如果查询不到值报no_data_found 异常,如果查询到多条数据报too_many_rows 异常
  --select add_months(e.hire_date,15*12) from employees e;
  select e.employee_id,e.hire_date into emp_no,hire_time from employees e where e.employee_id =emp_no;
  if add_months(hire_time,15*12) >sysdate then
    raise_application_error(-20001,'工龄未满15年');
  end if;
  update employees e set e.salary = e.salary+sal where e.employee_id = emp_no;
  commit;--提交事务
exception
  when no_data_found then
    dbms_output.put_line('员工不存在');
  end;







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值