zoukankan      html  css  js  c++  java
  • oracle中的控制语句

    一、条件语句
    1、流程控制-if else
    (1)if
    if 判断条件 then
          ...
    end if;
    (2)if-else
    if 判断条件 then
          ...
    else
          ...
    end if;
    (3)if-elsif
    if 判断条件1 then
          ...
    elsif 判断条件2 then
          ...
    elsif 判断条件n then
          ...
    end if;
    (4)if-elsif-else
    if 判断条件1 then
            ...
    elsif 判断条件2 then
            ...
    elsif 判断条件n then
            ...
    else
            ...
    end if;

    例:


    set serverout on;
    declare

          employee_number number;
    begin
         select count(*) into employee_count from employees where employee_age>30;
         if employee_count>10 then
             dbms_output.put_line('公司有年龄大于30的员工');
        else
            dbms_output.put_line('公司没有年龄大于30的员工');
        end if;
    end

    统计年龄大于30的员工信息,存在多个条件判断(elsif)时如下:

    declare

          employee_number number;
    begin
          select count(*) into employee_count from employees where employee_age>30;
          if employee_count=1 then
              dbms_output.put_line('公司有1名年龄大于30的员工');
         elsif employee_count>1 then
              dbms_output.put_line('公司有多年龄大于30的员工');
         else
             dbms_output.put_line('公司没有年龄大于30的员工');
         end if;
    end


    2、流程控制-case when
    case when 与if else 有相同的效果。当需要匹配的情况较多时,可以使流程控制更加清晰
    (1)case-when
    case expression
        when value1 then
              ...
        when value2 then
             ...
        when valuen then
             ...
    end case;
    (2)case-when-else
    case expression
        when value1 then
              ...
        when value2 then
             ...
        when valuen then
            ...
        else
           ...
    end case;

    例:


    declare employee_number number;
    begin
           select count(*) into employee_count from employees where employee_age>30;
          case employee_count
                when 1 then
                        dbms_output.put_line('公司有1名年龄大于30的员工');
                when 0 then
                        dbms_output.put_line('公司没有年龄大于30的员工');
               else
                      dbms_output.put_line('公司有多名年龄大于30的员工');
          end case
    end


    二、循环语句
    1、无条件循环-loop
    loop
        ...
    end loop;

    例:


    declare v_id number :=0;
           v_name varchar2(20);
    begin
      loop
          if v_id>=5 then
              exit;--退出循环
          end if;
          v_id:=v_id+1;

          select empolyee_name into v_name from employees where employee_id=v_id;

          dbms_output.put_line(v_id||'号员工是'||v_name);
      end loop;
    end;

    除了利用if判断退出循环外,还可以exit when的形式跳出循环


    2、while循环
    while 条件判断 loop
          循环操作
    end loop;

    while指定循环条件,其后仍然跟loop,此处的loop循环与无条件循环中的loop完全相同,只是增加了while条件而已。

    例:


    declare v_id number :=0;
           v_name varchar2(20);
    begin
    while v_id>=5
    loop

         v_id:=v_id+1;

        select empolyee_name into v_name from employees where employee_id=v_id;

         dbms_output.put_line(v_id||'号员工是'||v_name);
    end loop;
    end;


    3、for循环
    for .. in .. loop
         ...
    end loop;

    例:


    declare v_id number :=0;
            v_name varchar2(20);
    begin
    for v_id in 1..5
    loop

         v_id:=v_id+1;

         select empolyee_name into v_name from employees where employee_id=v_id;

         dbms_output.put_line(v_id||'号员工是'||v_name);
    end loop;
    end;


    4、循环控制关键字

    (1)continue:终止本次循环,进入下一次循环。

    (2)exit:终止本层循环,进入下一层循环。oracle中没有break关键字,用exit代替。

    (3)return:直接跳出存储过程或函数。

    转自:https://blog.csdn.net/qq_17503037/article/details/79261510

  • 相关阅读:
    flask 基础
    新的项目部署
    linux (01) linux基础
    linux (04) linux安装mysql
    linux (06) redis安装
    linux (09) nginx反向代理,负载均衡
    linux (08) nginx入门详解
    linux (07) redis详解
    linux(05) 编译安装py3
    spring-boot war包部署(二)
  • 原文地址:https://www.cnblogs.com/shujk/p/12498103.html
Copyright © 2011-2022 走看看