• pl/sql练习案例


    pl/sql练习案例

    1,写匿名块,输入三角形三个边的长度。在控制台打印三角形的面积。

    declare
    --声明变量
    v_w number(8,2):=&三角形的底边;
    v_h number(8,2):=&三角形的高;
    v_s number(8,2);
    begin
    --计算面积
    v_s:=(v_w*v_h)/2;
    --控制台输出
    dbms_output.put_line('三角形的面积:'||v_s);
    end;

    2,输入部门编号,在控制台打印这个部门的名称,总人数,平均工资(基本工资+奖金)

    declare
    --声明变量
    v_empno number(8,2):=&员工编号;
    v_count number(8,2);
    v_avg number(8,2);
    begin
    case v_empno
    when 10 then
      dbms_output.put_line('部门名称为:技术部');
    when 20 then
      dbms_output.put_line('部门名称为:销售部');
    when 30 then
      dbms_output.put_line('部门名称为:公关部');
    when 40 then
      dbms_output.put_line('部门名称为:开发部');
    else
      dbms_output.put_line('输入错误');
    end case;

    select count(*) into v_count from emp e where deptno=v_empno;
    dbms_output.put_line('总人数为:'||v_count);
     
    select avg(sal+nvl(comm,0)) into v_avg from emp
    where deptno=v_empno;
    dbms_output.put_line('平均工资为:'||v_avg);

    end;

    3,编写一个PL/SQL块,输出所有员工的员工姓名、员工号、工资和部门号

    这里用loop循环

    declare
    type emp_table_type is table of emp%rowtype
    index by binary_integer;
    v_emp emp_table_type;
    v_i number(8):=1;
    begin
    --这里用到了bulk collect into (批量效率的读取游标数据)
    select * bulk collect into v_emp from emp ;
    loop
      if v_i>v_emp.count then
        exit;
      end if;  
      dbms_output.put_line(
      '员工编号:'||v_emp(v_i).empno
      ||'员工姓名:'||v_emp(v_i).ename
      ||'工资:'||v_emp(v_i).sal
      ||'部门编号:'||v_emp(v_i).deptno
      );
      v_i:=v_i+1;
    end loop;

    end;

    4,编写一个PL/SQL块,输出所有比本部门平均工资高的员工信息。

    这里用for 循环

    declare
    type emp_table_type is table of emp%rowtype
    index by binary_integer;
    v_emp emp_table_type;
    begin
    select * bulk collect into v_emp from emp where sal>(
    select avg(sal) from emp where deptno=&no)and
    deptno=&no;
    for v_i in 1..v_emp.count
      loop
        dbms_output.put_line('员工编号'||v_emp(v_i).empno||
        '员工姓名'||v_emp(v_i).ename ||
        '上级领导'||v_emp(v_i).job ||
        '领导编号'||v_emp(v_i).mgr ||
        '入职日期'|| v_emp(v_i).hiredate ||
        '工资'||v_emp(v_i).sal ||
        '奖金'|| v_emp(v_i).comm ||
        '职位编号'||v_emp(v_i).deptno);
      end loop;

    end;

    5,编写一个PL/SQL块,输出所有员工及其部门领导的姓名、员工号及部门号。

    这里用while循环

    declare
    type emp_table_type is table of emp%rowtype
    index by binary_integer;
    v_emp emp_table_type;
    v_i number(8):=1;
    begin
    select * bulk collect into v_emp from emp;
    while v_i<v_emp.count
      loop
        dbms_output.put_line(
        '员工编号'||v_emp(v_i).empno||
        '员工姓名'||v_emp(v_i).ename||
        '领导姓名'||v_emp(v_i).job||
        '部门编号'||v_emp(v_i).deptno);
        v_i:=v_i+1;
      end loop;
    end;

    7,输入员工编号,根据员工的入职时间修改发放奖金类,大于等于6年的奖金为2000,小于6年的奖金是1500

    declare
    v_comm emp.comm%type;
    v_yyyy number(8);
    v_empno emp.empno%type:=&员工编号;
    begin
    --三个查询,先查询出当前日期和员工入职日期,然后计算出两者的相差
    --months_between 计算两个时间段相差的月份
    select months_between(
    (select sysdate from dual),
    (select hiredate from emp where empno=v_empno))
    into v_yyyy from dual;
    --查询出工资员工
    select comm into v_comm from emp where empno=v_empno;
    --将计算后的时间换算为年
    v_yyyy:=v_yyyy/12;
    --判断
    if v_yyyy>=6 then
      if v_comm is null then
        update emp set comm=(2000) where empno=v_empno;
      else
        update emp set comm=(v_comm+2000) where empno=v_empno;
      end if;
      dbms_output.put_line('发放奖金2000元');
    elsif v_yyyy<6 then
      if v_comm is null then
          update emp set comm=(1500) where empno=v_empno;
        else
          update emp set comm=(v_comm+1500) where empno=v_empno;
      end if;
      dbms_output.put_line('发放奖金1500元');
    else
      dbms_output.put_line('输入错误');
    end if;
    end;

    8,分别用loop ,while,for循环在控制台输出1到100的自然数。

    loop循环

    declare
    v_i number(8):=1;
    v_m varchar2(1000):='';
    begin
    loop
      if v_i>100 then
        exit;
      end if;
      v_m:=v_m||v_i||' ';
      v_i:=v_i+1;
    end loop;
    dbms_output.put_line(v_m);

    end;

    while 循环

    declare
    v_i number(8):=1;
    v_m varchar2(1000):='';
    begin
    while v_i<=100
      loop
          v_m:=v_m||v_i||' ';
      v_i:=v_i+1;
      end loop;
    dbms_output.put_line(v_m);

    end;

    for 循环

    declare
    
    v_m varchar2(1000):='';
    begin
      for v_i in 1..100
        loop
          v_m:=v_m||v_i||'  ';
        end loop;
        dbms_output.put_line(v_m);
    end;
    

    9,在控制台输出1到100以内的素数。

    declare
    v_i number(8);
    v_j number(8):=1;
    v_n number(8):=0;
    begin
      for v_i in 1..100
        loop
          while v_j<=v_i
            loop
              if mod(v_i,v_j)=0 then
                v_n:=v_n+1;
              end if;
              v_j:=v_j+1;
            end loop;
            v_j:=0;
            if v_n=2 then
              dbms_output.put_line(v_i);
            end if;
            v_n:=0;
        end loop;
    end;
    
  • 相关阅读:
    获取css信息
    html嵌套规则
    js获取ip地址
    match excel test search replace 用法
    js 宽和高
    判断类型 从零开始系列
    js随机数 从头开始系列
    苹果自带拼音转换方法
    iOS GCD 拾遗
    iOS用户响应者链的那些事儿
  • 原文地址:https://www.cnblogs.com/nnnnmmmm/p/10835897.html
Copyright © 2020-2023  润新知