日期:2014-05-16  浏览次数:20547 次

自定义记录类型游标
CREATE OR REPLACE PROCEDURE pr_attendleave(year     in varchar2,
                                           statdate IN date,
                                           empId    in varchar2) AS
BEGIN
  DECLARE
    司龄             number(10, 2);
    工龄             number(10, 2);
    核定年休假天数   number(10, 2);
    num4             number(10);
    核定年度最后一天 Date;
    核定年度第一天   Date;
    核定年度剩余天数 number(10);
    upyearDay        number(10, 2); --上一年剩余年假天数
    type ref_c_type is ref cursor; --定义游标
    type rec_aaa is record(
      empid          gcemployee.empid%type,
      joindate       gcemployee.joindate%type,
      startworkdate  gcemployee.startworkdate%type,
      MonthDeduction gcemployee.MonthDeduction%type); 
    empIdResult ref_c_type; --实例化这个游标类型 
    aaa         rec_aaa;
    ref_c_sql   varchar2(500); --动态游标的sql
  BEGIN
    ref_c_sql := 'select empid,joindate,startworkdate,MonthDeduction from gcemployee t where t.status=''ES1''';
    if (empId is not null) or empId <> '' then
      ref_c_sql := ref_c_sql || ' and t.empid=''' || empId || '''';
    end if;
    open empIdResult for ref_c_sql;
    LOOP
      fetch empIdResult into aaa;
      exit when empIdResult%notfound;
      司龄 := months_between(statdate, aaa.joindate); --司龄
      工龄 := months_between(statdate, aaa.startworkdate) -
            nvl(aaa.MonthDeduction, 0); --工龄
      if (year = '2008') then
        if (司龄 > 24) then
          --进本单位大于2年的 考虑工作日期
          if (工龄 > 21 * 12) then
            --判断大于21年的
            核定年休假天数 := 15;
          elsif (工龄 > 20 * 12) then
            --判断大于20 可能出现工龄界点
            核定年休假天数 := 10 * (1 - (工龄 - 240) / 12) + 15 * ((工龄 - 240) / 12);
          elsif (工龄 > 11 * 12) then
            --判断大于11年
            核定年休假天数 := 10;
          elsif (工龄 > 10 * 12) then
            --判断大于10年 可能出现工龄界点
            核定年休假天数 := 5 * (1 - (工龄 - 120) / 12) + 10 * ((工龄 - 120) / 12);
          else
            核定年休假天数 := 5; --判断小于10年的为5天
          end if;
        elsif (司龄 between 12 and 24) then
          --不到2年的 需要计算 百分比
          if (工龄 > 21 * 12) then
            --判断大于21年的
            核定年休假天数 := 15;
          elsif (工龄 > 20 * 12) then
            --判断大于20
            核定年休假天数 := 10 * (1 - (工龄 - 240) / 12) + 15 * ((工龄 - 240) / 12);
          elsif (工龄 > 11 * 12) then
            --判断大于11年
            核定年休假天数 := 10;
          elsif (工龄 > 10 * 12) then
            核定年休假天数 := 5 * (1 - (工龄 - 120) / 12) + 10 * ((工龄 - 120) / 12);
          else
            核定年休假天数 := 5; --判断小于10年的为5天
          end if;
        else
          --不到1年的均为0
          核定年休假天数 := 0;
        end if;
      else
        --大于2008年的
        if (司龄 > 24) then
          --进本单位大于2年的 考虑工作日期
          if (工龄 > 21 * 12) then
            --判断大于21年的
            核定年休假天数 := 15;
          elsif (工龄 > 20 * 12) then
            --判断大于20 可能出现工龄界点
            核定年休假天数 := 10 * (1 - (工龄 - 240) / 12) + 15 * ((工龄 - 240) / 12);
          elsif (工龄 > 11 * 12) then
            --判断大于11年
            核定年休假天数 := 10;
          elsif (工龄 > 10 * 12) then
            --判断大于10年 可能出现工龄界点
            核定年休假天数 := 5 * (1 - (工龄 - 120) / 12) + 10 * ((工龄 - 120) / 12);
          else
            核定年休假天数 := 5; --判断小于10年的为5天
          end if;
        elsif (司龄 between 12 and 24) then
          --不到2年的 需要计算 百分比
          if (工龄 > 21 * 12) then
            --判断大于21年的
            核定年休假天数 := 15;
          elsif (工龄 > 20 * 12) then
            --判断大于20
            核定年休假天数 := 10 * (1 - (工龄 - 240) / 12) + 15 * ((工龄 - 240) / 12);
          elsif (工龄 > 11 * 12) then
            --判断大于11年
            核定年休假天数 := 10;
          elsif (工龄 > 10 * 12) then
            核定年休假天数 := 5 * (1 - (工龄 - 120) / 12) + 10 * ((工龄 - 120) / 12);
          elsif (工龄 > 24) then
            核定年休假天数 := 5; --判断小于10年的为5天
          elsif (工龄 > 12) then
            核定年休假天数 := 5 * (工龄 - 12)