• 【PL/SQL练习】控制结构


    1.if判断:

    if-then-end if:

    SQL> declare
      2  
      3   v_ename emp.ename%type;
      4   v_sal  emp.sal%type;
      5  
      6  begin
      7  
      8   select ename,sal into v_ename,v_sal from emp where empno=&#
      9   dbms_output.put_line('Old salary is: '||v_sal);
     10    if v_sal < 8000 then
     11       update emp set sal=sal+1000 where empno=&num;
     12       commit;
     13    end if;
     14   select ename,sal into v_ename,v_sal from emp where empno=&num;
     15       dbms_output.put_line('New salary is: '||v_sal);
     16  
     17  end;

    if-then-else-end if:

    SQL> declare
      2  
      3   v_ename emp.ename%type;
      4   v_sal  emp.sal%type;
      5  
      6  begin
      7  
      8   select ename,sal into v_ename,v_sal from emp where empno=&&num;
      9   dbms_output.put_line('Old salary is: '||v_sal);
     10    if v_sal < 8000 then
     11       update emp set sal=sal+1000 where empno=&num;
     12       commit;
     13    else
     14       update emp set sal=sal+500 where empno=&num;
     15       commit;
     16    end if;
     17   select ename,sal into v_ename,v_sal from emp where empno=&num;
     18       dbms_output.put_line('New salary is: '||v_sal);
     19  
     20  end;

    if-then-elsif-else-end if:

    SQL> declare
      2  
      3   v_ename emp.ename%type;
      4   v_sal  emp.sal%type;
      5   v_job  emp.job%type;
      6  
      7  begin
      8  
      9   select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&&num;
     10   dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal);
     11    if  v_job='SALESMAN' then
     12       update emp set sal=sal+1000 where empno=&num;
     13       commit;
     14    elsif  v_job='CLERK' then
     15        update emp set sal=sal+800 where empno=&num;
     16       commit;
     17    else
     18       update emp set sal=sal+500 where empno=&num;
     19       commit;
     20    end if;
     21   select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&num;
     22       dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_sal);
     23  
     24  end;
    SQL> declare
      2  
      3   v_ename emp.ename%type;
      4   v_sal  emp.sal%type;
      5   v_job  emp.job%type;
      6  
      7  begin
      8  
      9   select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&&num;
     10   dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal);
     11    if  v_job='SALESMAN' then
     12       update emp set sal=sal+1000 where empno=&num;
     13       commit;
     14    elsif  v_job='CLERK' then
     15        update emp set sal=sal+800 where empno=&num;
     16       commit;
     17    else
     18       update emp set sal=sal+500 where empno=&num;
     19       commit;
     20    end if;
     21   select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&num;
     22       dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_sal);
     23  
     24  end;

    2.case分支判断:

    SQL> declare
      2      v_ename emp.ename%type;
      3      v_job emp.job%type;
      4      v_sal emp.sal%type;
      5      v_out varchar2(10);
      6  
      7     begin
      8       select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&&num;
      9       dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal);
     10  
     11       v_out:=case v_job
     12           when 'CLERK' THEN v_SAL+100
     13           WHEN 'SALESMAN' THEN v_SAL+200
     14      END ;
     15        select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&num;
     16        dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT);
     17         dbms_output.put_line(v_out);
     18     end;
    SQL> declare
      2      v_ename emp.ename%type;
      3      v_job emp.job%type;
      4      v_sal emp.sal%type;
      5      v_out varchar2(10);
      6  
      7     begin
      8       select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&&num;
      9       dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal);
     10  
     11       v_out:=case v_job
     12           when 'CLERK' THEN v_SAL+100
     13           WHEN 'SALESMAN' THEN v_SAL+200
     14      END ;
     15        select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&num;
     16        dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT);
     17  
     18     end;
    SQL> declare
      2      v_ename emp.ename%type;
      3      v_job emp.job%type;
      4      v_sal emp.sal%type;
      5      v_out varchar2(10);
      6  
      7     begin
      8       select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&&num;
      9       dbms_output.put_line('JOB is: '||v_job||';'||'Old salary is: '||v_sal);
     10  
     11       v_out:=case v_job
     12           when 'CLERK' THEN v_SAL+100
     13           WHEN 'SALESMAN' THEN v_SAL+200
     14      END ;
     15        select ename,job,sal into v_ename,v_job,v_sal from emp where empno=&num;
     16        dbms_output.put_line('JOB is: '||v_job||';'||'New salary is: '||v_OUT);
     17  
     18     end;

    在sql中使用case:

    SQL>  select ename,job,sal,
      2      CASE
      3     WHEN JOB='CLERK' THEN SAL+100
      4     WHEN JOB='MANAGER' THEN SAL+50
      5     WHEN JOB='SALESMAN' THEN SAL+300
      6     ELSE
      7      SAL
      8    END AS ADD_SAL
      9     FROM EMP;

    decode 语句:

    SQL> select ename,sal,JOB,
      2          decode (job ,'CLERK',SAL+100,
      3                        'SALESMAN',SAL+300,
      4                         'MANAGER',SAL+50,
      5                         SAL) ADD_SAL
      6        FROM EMP;

    3.循环语句:

    案例:在表中插入数据

    ①loop基本循环

    SQL> declare
      2  v_num number :=1;
      3  v_count number;
      4  
      5  begin
      6  
      7   loop
      8       insert into t1 values (v_num,'tom'||v_num ,'ok');
      9        v_num :=v_num + 1;
     10  
     11   exit when v_num >10;
     12  
     13   end loop;
     14   commit;
     15  
     16   select count(*) into v_count from t1;
     17  
     18   dbms_output.put_line('T1 rows is :'||v_count);
     19  
     20  end;

    ②for 循环:

    SQL> declare
      2  v_count number;
      3  
      4  begin
      5  
      6  for i in 1..10
      7    loop
      8       insert into t1 values (i,'tom'||i ,'ok');
      9   end loop;
     10   commit;
     11  
     12   select count(*) into v_count from t1;
     13  
     14   dbms_output.put_line('T1 rows is :'||v_count);
     15  
     16  end;

    ③while 循环:

    SQL> declare
      2  v_num number := 1;
      3  v_count number  ;
      4  
      5  begin
      6  
      7   while v_num <=10 loop
      8       insert into t1 values (v_num,'tom'||v_num ,'ok');
      9       v_num := v_num + 1;
     10   end loop;
     11   commit;
     12  
     13   select count(*) into v_count from t1;
     14  
     15   dbms_output.put_line('T1 rows is :'||v_count);
     16  
     17  end;
  • 相关阅读:
    进程管理
    磁盘管理
    用户组管理
    Idea 导入(import)项目和打开(open)项目的区别
    SqlServer--转换varchr值‘2993296307’时溢出了整数列 和 修改 字段类型
    C#--Winform--图标控件Chart详解
    SqlServer--存储过程--自定义存储过程
    SqlServer--存储过程--系统和扩展存储过程(不常用)
    SqlServer--视图
    C#--SqlServer--sql语句拼接和带参数的SQL语句
  • 原文地址:https://www.cnblogs.com/tomatoes-/p/6104361.html
Copyright © 2020-2023  润新知