PL/SQL笔记整理

–1输出小于等于指定参数的所有斐波那契数列,比如输入参数为10Procedure名称:fibonacci_output
–参数:p_limit,0、1、1、2、3、5、8、13
–说明:参数为输出限制,最后数据不超过该参数,使用dbms_output.put_line来输出

  procedure fibonacci_output(p_limit number) is
    i    number := 0;
    t    number := 1;
    temp number := 1;
  begin
    dbms_output.put_line(i);
    while temp < p_limit loop
      dbms_output.put_line(temp);
      temp := i + t;
      i    := t;
      t    := temp;
    end loop;
  end;

–2. 输入一个日期,计算该日期在该年内第几天
–Procedure名称:date_num
–参数:p_date,类型为字符,格式yyyy-mm-dd
–说明:使用dbms_output.put_line来输出(5分)
–失误,没有看清题意,参数传错了

  Procedure date_num(p_date varchar2) is
    first_day date;
    n         number := 0;
  begin
    first_day := TRUNC(to_date(p_date,'yyyy-mm-dd'), 'YEAR');
    n         := to_date(p_date,'yyyy-mm-dd') - first_day + 1;
    dbms_output.put_line('the date is the ' || n || 'th day of the year');
  end;

–3. 个人所得税计算:个人所得税分段计算,具体分段如下:
/*0-3500 免征税
3501-4500 10%
4501-9000 20%
9000以上 25%
输入一个参数,计算应该缴纳的税收为多少
function名称:tax_calc
参数:p_salary
返回值:需要缴纳的税收*/

  function tax_calc(p_salary number) return number is
    tax_sum number;
  begin
    if p_salary > 0 and p_salary < 3500 then
      tax_sum := 0;
    elsif p_salary > 3501 and p_salary < 4500 then
      tax_sum := (p_salary-3500) * 0.1;
    elsif p_salary > 4501 and p_salary < 9000 then
      tax_sum := 1000*0.1+(p_salary-4500) * 0.2;
    elsif p_salary >= 9000 then
      tax_sum := 1000*0.1+4500*0.2+(p_salary-9000) * 0.25;
    end if;
    dbms_output.put_line('the tax you should pay is : ' || tax_sum);
    return tax_sum;
  end;
  1. A/B/C三种物品的价格分别为p_a_price,p_b_price,p_c_price,总金额为p_total,计算A/B/C数量(整数)分别为多少,
    使得A/B/C总金额加起来不超过p_total并且最接近p_total(如果有多个组合,只需要得出一个组合即可),
    在包里写一个函数。
    procedure名称:quantity_calc
    参数:p_a_price
    p_b_price
    p_c_price
    p_total
    x_a_quantity
    x_b_quantity
    x_c_quantity
    p开头的为输入类型参数,x开头为输出类型参数
    (15分)
  procedure  quantity_calc(p_a_price in number,p_b_price in number,p_c_price in number,p_total in number,
                    x_a_quantity out number,x_b_quantity out number, x_c_quantity out number ) is
  i number:=0;
  j number:=0;
  t number:=0;
  begin 
       for i in 0..trunc(p_total/p_a_price,0) loop
           for j in 0..trunc(p_total/p_b_price,0) loop
               for t in 0..trunc(p_total/p_c_price,0) loop
                   if(i*p_a_price+j*p_b_price+t*p_c_price)=p_total then 
                        x_a_quantity:=i;
                        x_b_quantity:=j;
                        x_c_quantity:=t;
                    end if;
               end loop;
            end loop;
         end loop;
    end;

–1. 创建一个过程,给所有成绩低于该课程平均分的男生加2分,女生加3分,加完分后,如果超过100分,
–就更新为100分。如果加分后的学生成绩低于60分,则输出学生的信息(按学号顺序)。处理成功则提交事物;失败则回滚事物。(10分)
–Procedure名称:update_score
–说明:使用dbms_output.put_line输出,输出学生编号、学生姓名

  Procedure update_score is
    CURSOR cur_core IS
      select hsc.student_no, hs.student_name, hs.student_gender, hsc.core
        from (SELECT hsc.course_no, avg(hsc.core) AVG_C
                FROM hand_student_core hsc
               group by (hsc.course_no)) H,
             hand_student hs,
             hand_student_core hsc
       where hsc.core < H.AVG_C
         and hsc.course_no = H.course_no
         AND hsc.student_no = hs.student_no
         FOR UPDATE OF hsc.core;
  --TYPE core_ind IS TABLE OF cur_core INDEX BY hand_student_core.student_no%TYPE;   
  begin
    DBMS_OUTPUT.put_line('学生编号  学生姓名');
    for rec_core in cur_core loop
      if (rec_core.core + 2) > 60 and rec_core.student_gender = '男' then
        UPDATE hand_student_core hsc
           SET hsc.core = rec_core.core + 2
         WHERE CURRENT OF cur_core;
      elsif (rec_core.core + 3) > 60 and rec_core.student_gender = '女' then
        UPDATE hand_student_core hsc
           SET hsc.core = rec_core.core + 3
         WHERE CURRENT OF cur_core;
      elsif ((rec_core.core + 2) > 100 and rec_core.student_gender = '男') OR
            ((rec_core.core + 3) < 100 and rec_core.student_gender = '女') then
        UPDATE hand_student_core hsc
           SET hsc.core = 100
         WHERE CURRENT OF cur_core;
      elsif ((rec_core.core + 2) < 60 and rec_core.student_gender = '男') OR
            ((rec_core.core + 3) < 60 and rec_core.student_gender = '女') THEN
            --core_ind(rec_core.student_no).student_no=rec_core.student_no;
            --core_ind(rec_core.student_no).student_name= rec_core.student_name;     
        DBMS_OUTPUT.put_line(rec_core.student_no || '  ' ||rec_core.student_name);
        --如何按学号顺序实现,没头绪。。
      END IF;
    END LOOP;
   commit;--没提交上去,好奇怪。。

  EXCEPTION
    WHEN OTHERS THEN
      ROLLBACK;
  END;

–2. 写一个procedure,对学分小于6分的补课程,
–成绩60-70分的1个学分,70-80的2个学分,80以上的3个学分。(10分)
–Procedure名称:student_score
–说明:补充的课程必须是该学生没选修过的课程,补的课程成绩默认85分,
–补课程的数量要使得这个学生的学分大于等6分,比如该学生之前学分2分,那就必须补2个课程,如果之前学分3分,则只需补一个课程。
写了好久才摸索出来,自己有点low,如果有大神有更好的算法,还请不吝赐教。。。

  procedure student_score is
    cursor course_to_learn is
      select h.student_no, sum(h.c) sum_c
        from (select hsc.student_no,
                     hsc.course_no,
                     case
                       when hsc.core between 60 and 69 then
                        1
                       when hsc.core between 70 and 79 then
                        2
                       when hsc.core > 80 then
                        3
                       when hsc.core < 60 then
                        0
                     end c
                from hand_student_core hsc) h
       group by (h.student_no);
    need_core number;
    cursor c_minus(s varchar2) is
      select TA.course_no
        from ((select hc.course_no from hand_course hc) minus
              (select hsc.course_no
                 from hand_student_core hsc
                where hsc.student_no = s)) TA;
    i number;
  begin
    for rec in course_to_learn loop
      if rec.sum_c < 6 then
        need_core := 6 - rec.sum_c;
        need_core := ceil(need_core / 3);
        dbms_output.put_line('学生学号:' || rec.student_no);
        dbms_output.put_line('需要修的课程数:' || need_core);
        dbms_output.put_line('可选修课程如下:' || need_core);
        i := 0;
        FOR minu_c in c_minus(rec.student_no) loop
          i := i + 1;
          dbms_output.put_line(minu_c.course_no);
          exit when i = need_core;
        end loop;
      end if;
    end loop;
  end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值