oralce第五天——控制语句

--oracle控制语句
  --1.基本语法
  set serverout on;--定义在控制台输出
  begin
    --此处写需要执行的语句
    dbms_output.put_line('hello'); --打印输出(put_line换行输出)
  end;
  --必须加上/结尾
  /
  
  -- 2.定义参数
  set serverout on;
  --定义参数并赋值
  declare n number := 1;
          v varchar2(20) := 'world';
  begin
    dbms_output.put_line('hello'||v||n);--此处字符串连接用||
  end;
  /
  
  --3.带控制语句
    --IF语句 判断
      set serverout on;
      declare emp_count number;
      begin
        select count(*) into emp_count from emp where sal >= 2000;
        if(emp_count = 1) then
           dbms_output.put_line('有1个员工基本工资大于2000');
        else if (emp_count > 1) then
           dbms_output.put_line(''||emp_count||'个员工基本工资大于2000');
        else
           dbms_output.put_line('没有员工基本工资大于2000');
        end if;
        --有多少个if就需要多少个end if与之匹配
        end if;
      end; 
      / 
    
    --CASE WHEN 流程控制
      set serverout on;
      declare emp_count number;
      begin
        select count(*) into emp_count from emp where sal >= 3000;
        case emp_count
          when 0 then dbms_output.put_line('没有员工基本工资大于3000');
          when 1 then dbms_output.put_line('有1个员工基本工资大于3000');
          when 2 then dbms_output.put_line('有2个员工基本工资大于3000');
          when 3 then dbms_output.put_line('有3个员工基本工资大于3000');
          else dbms_output.put_line('超过3个员工基本工资大于3000');--默认值
        end case;--结束case
      end; 
      /
      
    --循环语句 loop
      set serverout on;
      --定义变量
      declare g_id number := 2;
              g_losal number;
              g_hisal number;  
      begin
        --循环开始
        loop
          --判断然后退出循环(loop需要用exit退出)
          if(g_id > 4) then
             exit;
          end if;
          --业务逻辑
          select losal,hisal into g_losal,g_hisal from salgrade where grade = g_id;
          dbms_output.put_line(g_id || '等级的最低薪资' || g_losal || '最高薪资' || g_hisal);
          --循环条件自增
          g_id := g_id + 1;  
        --结束循环  
        end loop;
      end;
      /
      
    --循环语句 while
      set serverout on;
      --定义变量
      declare g_id number := 2;
              g_losal number;
              g_hisal number;
      begin
        while g_id < 5 loop
          --业务逻辑
          select losal,hisal into g_losal,g_hisal from salgrade where grade = g_id;
          dbms_output.put_line(g_id || '等级的最低薪资' || g_losal || '最高薪资' || g_hisal);   
          --循环条件自增
          g_id := g_id + 1;
        end loop;
      end;  
      /
      -- loop 和 while的区别在于loop需要用exit退出,而while不需要
      
    --循环语句 for
      set serverout on;
        --定义变量
        declare g_losal number;
                g_hisal number;
      begin
        --循环开始,for循环自带自增的功能,所以最简单
        for g_id in 2..4 loop
          --业务逻辑
          select losal,hisal into g_losal,g_hisal from salgrade where grade = g_id;
          dbms_output.put_line(g_id || '等级的最低薪资' || g_losal || '最高薪资' || g_hisal);   
        end loop; 
      end;
      
  --总结:loop,while和for循环,其中for循环最简单(常用)

 

posted @ 2016-12-07 22:19  Revel  阅读(207)  评论(0)    收藏  举报