zoukankan      html  css  js  c++  java
  • Oracle学习操作(3)

    一、if条件语句                                           

    1 set serverout on;
    2 declare n number:=1;
    3            v varchar(20):='world';
    4 begin
    5     dbms_output.put_line('hello'||n||v);
    6 end;
    7 /
    8 
    9 hello1world
    declare emp_count number;
    begin
        select count(*) into emp_count from emp where sal >= 3000;
        if(emp_count>0) then
            dbms_output.put_line(''||emp_count||'员工的基本薪资大于等于3000');
        else
            dbms_output.put_line('没有员工的基本薪资大于等于3000');
        end if;
    end;

    有3个员工的基本薪资大于等于3000

    if elseif else if :

    SQL> declare emp_count number;
      2  begin
      3     select count(*) into emp_count from emp where sal >= 3000;
      4     if(emp_count=1) then
      5             dbms_output.put_line('有1个员工的基本薪资大于等于3000');
      6     else if(emp_count>1) then
      7             dbms_output.put_line('超过1个员工的基本薪资大于等于3000');
      8     else
      9             dbms_output.put_line('没有员工的基本薪资大于等于3000');
     10     end if;
     11     end if;
     12  end;
     13  /
    超过1个员工的基本薪资大于等于3000
    
    PL/SQL 过程已成功完成。

    二、case when流程控制语句                                      

    SQL> declare emp_count number;
      2  begin
      3     select count(*) into emp_count from emp where sal >= 3000;
      4     case emp_count
      5             when 0 then dbms_output.put_line('没有员工的基本薪资大于等于3000');
      6             when 1 then dbms_output.put_line('有1个员工的基本薪资大于等于3000');
      7             when 2 then dbms_output.put_line('有2个员工的基本薪资大于等于3000');
      8             when 3 then dbms_output.put_line('有3个员工的基本薪资大于等于3000');
      9             else dbms_output.put_line('超过3个员工的基本薪资大于等于3000');
     10     end case;
     11  end;
     12  /
    有3个员工的基本薪资大于等于3000
    
    PL/SQL 过程已成功完成。

    三、循环语句                                                

    1.无条件循环 loop:

    salgrade表:

    现在循环grade从2到4,打印出最低薪资,和最高薪资:

    SQL> declare g_id number:=2;
      2             g_losal number;
      3             g_hisal number;
      4  begin
      5     loop
      6             if(g_id>4) then
      7                     exit;
      8             end if;
      9
     10             select losal,hisal into g_losal, g_hisal from salgrade where grade = g_id;
     11             dbms_output.put_line(g_id||'等级的最低薪资'||g_losal||',最高薪资'||g_hisal);
     12
     13             g_id := g_id + 1;
     14     end loop;
     15  end;
     16  /
    2等级的最低薪资1201,最高薪资1400
    3等级的最低薪资1401,最高薪资2000
    4等级的最低薪资2001,最高薪资3000
    
    PL/SQL 过程已成功完成。

    2.while循环:

    SQL> declare g_id number:=2;
      2             g_losal number;
      3             g_hisal number;
      4  begin
      5     while(g_id<5) loop
      6             select losal,hisal into g_losal, g_hisal from salgrade where grade = g_id;
      7             dbms_output.put_line(g_id||'等级的最低薪资'||g_losal||',最高薪资'||g_hisal);
      8
      9             g_id := g_id + 1;
     10     end loop;
     11  end;
     12  /
    2等级的最低薪资1201,最高薪资1400
    3等级的最低薪资1401,最高薪资2000
    4等级的最低薪资2001,最高薪资3000
    
    PL/SQL 过程已成功完成。

    3、for循环:

    SQL> declare g_losal number;
      2             g_hisal number;
      3  begin
      4     for g_id in 2..4 loop
      5             select losal,hisal into g_losal, g_hisal from salgrade where grade = g_id;
      6             dbms_output.put_line(g_id||'等级的最低薪资'||g_losal||',最高薪资'||g_hisal);
      7     end loop;
      8  end;
      9  /
    2等级的最低薪资1201,最高薪资1400
    3等级的最低薪资1401,最高薪资2000
    4等级的最低薪资2001,最高薪资3000
    
    PL/SQL 过程已成功完成。
  • 相关阅读:
    Mysql索引类型
    Linux 查看进程
    Element is not clickable at point error in chrome
    org.apache.commons.lang.StringUtils 中 Join 函数
    接口测试之webservice介绍
    Java 中正确使用 hashCode 和 equals 方法
    HTTP协议基础
    使用Groovy处理SoapUI中Json response
    SoapUI中读取法文字符
    SoapUI中XML解析
  • 原文地址:https://www.cnblogs.com/tenWood/p/6629659.html
Copyright © 2011-2022 走看看