分支

declare --声明变量

a varchar2(20);
b number(10);
c number(10);

begin  --开始
  
  a := '小明';
  dbms_output.put_line(a);
  b := 2;
  c := 4;
  if b > c then 
    dbms_output.put_line('b大于c');
    elsif b < c then 
      dbms_output.put_line('b小于c');
      else 
        dbms_output.put_line('b等于c');
  end if;
  
end; --结束

循环 和 异常

declare
 m number(10);
 sname varchar2(40);
 nozero exception;  --自定义异常
begin
  m := 60; -- := 实现赋值操作
  if m = 60 then 
    raise nozero; --如果满足这个条件,就抛出这个异常(自定义的)
  end if;
  
  exception 
    when nozero then --抛出这个异常的输出
       dbms_output.put_line('m不能为60');
    when others then --发生其他异常
       dbms_output.put_line('其他异常');
  
  
  
  
  /*loop --loop循环
    exit when m < 0; --exit when 满足某条件时候跳出循环
    dbms_output.put_line(m);
    m := m-1;
   end loop;*/
   
  /* while m>0 loop -- 用while ... loop实现循环,中间为需要满足的条件
     dbms_output.put_line(m);
     m := m-1;
     end loop;
  */
 /* for n in -1..2 loop -- 1..2 是指变量的范围,只能用来遍历整数
    dbms_output.put_line(n);
  end loop;*/
  
 /* select s.name into sname from z_student s where s.id = m; --查出ID为m的学生姓名,通过into赋值给 sname
  dbms_output.put_line('学生姓名为' || sname);  

  
  exception  --抛出异常
    when no_data_found then  -- no_data_found 系统自带的异常
     dbms_output.put_line('没有查到数据');  
     */
      
  
end;

 

posted on 2017-08-14 21:07  donghongbo  阅读(213)  评论(0编辑  收藏  举报