oracle proc

来源:互联网 发布:冰川网络 郑州 编辑:程序博客网 时间:2024/06/07 15:16
CREATE OR REPLACE PROCEDURE user_learn_stat_p(v_in_user_id IN number) AS
  --参数IN表示输入参数,
  --OUT表示输入参数,类型可以使用任意Oracle中的合法类型。
  --定义变量
  v_classroom_id               number(10); --课程id
  v_user_id                    number(10); --课程id
  vs_msg                       varchar2(4000); --错误信息变量
  dept_no_array_c              dbms_sql.Number_Table; --数组完成
  ilm_classroom_id             dbms_sql.Number_Table; --综合学习课程id
  dept_no_array_t              dbms_sql.Number_Table; --数组总计
  dept_no_array_classroom      dbms_sql.Number_Table; --数组classroomid
  dept_no_array_classroom_name dbms_sql.Varchar2_Table; --数组classroom名称

  v_classroom_count number(10);

  v_i number(10);

  v_j number(10);
  v_k number(10);
  v_kk number(10);

  v_ret number(10);

  --定义游标(简单的说就是一个可以遍历的结果集)

  CURSOR cur_1 IS
 
    select C.id,
          
           C.name,
          
           C.type,
          
           EV.start_date,
          
           C.rco_id,
          
           NVL(R.title, '') as rco_title,
          
           'Y' as is_enrolled,
          
           C.location,
          
           C.is_sample,
          
           C.is_browsable,
          
           'Y' as is_published,
          
           DECODE(C.cal_ev_featured, null, 'N', 'Y') as is_featured,
          
           cg.name as content_group_name,
          
           E.cert_period_start_date,
          
           E.cert_period_end_date,
          
           E.initial_certification_days,
          
           E.renewal_certification_days
   
      from classroom C,
          
           cal_events EV,
          
           rco R,
          
           enrollment E,
          
           privilege_ref P,
          
           content_group cg
   
     where P.user_id = v_in_user_id
         
       and P.target_id = E.classroom_id
         
       and E.user_id = v_in_user_id
         
       and E.classroom_id = C.id
         
       and E.is_deleted = 'N'
         
       and C.cal_event_id = EV.id
         
       and C.is_published = 'Y'
         
       and C.is_hidden = 'N'
         
       and (EV.end_date > sysdate OR EV.end_date IS NULL OR
          
           EV.repeat_until > sysdate)
         
       and C.content_group_id = cg.id
         
       and C.rco_id = R.id(+)
       and C.id not in(
        SELECT /* IlmEnrolledView */
        --c.name as cname,
        c.id as cid
      FROM GROUP_ENROLLMENT     GE,
           CLASSROOM_GROUP      CG,
           classroom_group_xref x,
           classroom            c
     WHERE GE.CLASSROOM_GROUP_ID = CG.ID
       and x.class_id = c.id
       and cg.id = x.group_id
       AND CG.IS_PUBLISHED = 'Y'
       AND GE.USER_ID = v_in_user_id
       AND GE.IS_DELETED = 'N'
       AND GE.SITE_ID = CG.SITE_ID
       AND CG.TYPE = 'I'
       );

  CURSOR cur_2 IS
    SELECT /* IlmEnrolledView */
     c.name as cname, c.id as cid
      FROM GROUP_ENROLLMENT     GE,
           CLASSROOM_GROUP      CG,
           classroom_group_xref x,
           classroom            c
     WHERE GE.CLASSROOM_GROUP_ID = CG.ID
       and x.class_id = c.id
       and cg.id = x.group_id
       AND CG.IS_PUBLISHED = 'Y'
       AND GE.USER_ID = v_in_user_id
       AND GE.IS_DELETED = 'N'
       AND GE.SITE_ID = CG.SITE_ID
       AND CG.TYPE = 'I';

BEGIN

  select count(*)
 
    into v_classroom_count
 
    from classroom C,
        
         cal_events EV,
        
         rco R,
        
         enrollment E,
        
         privilege_ref P,
        
         content_group cg
 
   where P.user_id = v_in_user_id
       
     and P.target_id = E.classroom_id
       
     and E.user_id = v_in_user_id
       
     and E.classroom_id = C.id
       
     and E.is_deleted = 'N'
       
     and C.cal_event_id = EV.id
       
     and C.is_published = 'Y'
       
     and C.is_hidden = 'N'
       
     and (EV.end_date > sysdate OR EV.end_date IS NULL OR
        
         EV.repeat_until > sysdate)
       
     and C.content_group_id = cg.id
       
     and C.rco_id = R.id(+)
      and C.id not in(
        SELECT /* IlmEnrolledView */
        --c.name as cname,
        c.id as cid
      FROM GROUP_ENROLLMENT     GE,
           CLASSROOM_GROUP      CG,
           classroom_group_xref x,
           classroom            c
     WHERE GE.CLASSROOM_GROUP_ID = CG.ID
       and x.class_id = c.id
       and cg.id = x.group_id
       AND CG.IS_PUBLISHED = 'Y'
       AND GE.USER_ID = v_in_user_id
       AND GE.IS_DELETED = 'N'
       AND GE.SITE_ID = CG.SITE_ID
       AND CG.TYPE = 'I'
       );

  --用输入参数给变量赋初值,用到了Oralce的SUBSTR TO_CHAR ADD_MONTHS TO_DATE 等很常用的函数。

  v_user_id := v_in_user_id;

  --然后用内置的DBMS_OUTPUT对象的put_line方法打印出影响的记录行数,其中用到一个系统变量SQL%rowcount

  --DBMS_OUTPUT.put_line('del上月记录='||SQL%rowcount||'条');

  --遍历游标处理后更新到表。遍历游标有几种方法,用for语句是其中比较直观的一种。
 
 
   v_k := 0;

  FOR rec2 IN cur_2 LOOP
    v_k := v_k + 1;
    ilm_classroom_id(v_k) := rec2.cid;
      dbms_output.put_line('综合学习'||ilm_classroom_id(v_k));
  END LOOP;

  v_i := 0;
 
  FOR rec IN cur_1 LOOP
 
    v_i := v_i + 1;
 
    v_classroom_id := rec.id;
 
    dept_no_array_classroom(v_i) := rec.id;
 
    select mm.name
      into dept_no_array_classroom_name(v_i)
      from classroom mm
   
     where mm.id = v_classroom_id;
 
    SELECT /* UserStatusView */
   
     completed, total
   
      into dept_no_array_c(v_i), dept_no_array_t(v_i)
   
      FROM (SELECT SUM(DECODE(nvl(P.STATUS, 'N'), 'N', 1, 0)) AS NOT_ATTEMPTED,
                  
                   SUM(DECODE(P.STATUS, 'C', 1, 0)) AS COMPLETED,
                  
                   SUM(DECODE(P.STATUS, 'P', 1, 0)) AS PASSED,
                  
                   SUM(DECODE(P.STATUS, 'I', 1, 0)) AS INCOMPLETE,
                  
                   SUM(DECODE(P.STATUS, 'F', 1, 0)) AS FAILED,
                  
                   COUNT(P.child_id) AS TOTAL
           
              FROM (select x.child_id, P.status
                   
                      from tree_xref x,
                          
                           performance p,
                          
                           rco,
                          
                           classroom c,
                          
                           enrollment E
                   
                     where c.id = v_classroom_id
                         
                       and x.parent_id = c.rco_id
                         
                       and x.child_id = rco.id
                         
                       and p.rco_id(+) = rco.source_rco_id
                         
                       and p.user_id(+) = v_user_id
                         
                       and rco.is_published = 'Y'
                         
                       and E.user_id(+) = v_user_id
                         
                       and E.classroom_id(+) = C.id
                         
                       and E.cert_period_start_date is null
                   
                    UNION
                   
                    select x.child_id, P.status
                   
                      from tree_xref x,
                          
                           certification_performance p,
                          
                           rco,
                          
                           classroom c,
                          
                           enrollment E
                   
                     where c.id = v_classroom_id
                         
                       and x.parent_id = c.rco_id
                         
                       and x.child_id = rco.id
                         
                       and p.rco_id(+) = rco.source_rco_id
                         
                       and p.user_id(+) = v_in_user_id
                         
                       and p.classroom_id(+) = v_classroom_id
                         
                       and p.end_date(+) is null
                         
                       and rco.is_published = 'Y'
                         
                       and E.user_id = v_user_id
                         
                       and E.classroom_id = C.id
                         
                       and E.cert_period_start_date is not null) P);
 
  END LOOP;

  dbms_output.put_line('课程总计');

  dbms_output.put_line(v_classroom_count);

  v_ret := 0;

  FOR v_j in 1 .. v_classroom_count loop

    if dept_no_array_t(v_j) = 0 or dept_no_array_c(v_j) = 0 or
       dept_no_array_c(v_j) / dept_no_array_t(v_j) < 1 then
      v_ret := v_ret + 1;
    end if;
 
    dbms_output.put_line('!!!');
 
    dbms_output.put_line(v_j);
 
    dbms_output.put_line(dept_no_array_classroom(v_j));
 
    dbms_output.put_line(dept_no_array_classroom_name(v_j));
 
    dbms_output.put_line('完成');
 
    dbms_output.put_line(dept_no_array_c(v_j));
 
    dbms_output.put_line('总计');
 
    dbms_output.put_line(dept_no_array_t(v_j));
 
    dbms_output.put_line('百分比');
 
    dbms_output.put_line(dept_no_array_c(v_j) / dept_no_array_t(v_j));
 
    dbms_output.put_line('&&&');
    end loop;
  dbms_output.put_line('未完成课程');

  dbms_output.put_line(v_ret);

  --错误处理部分。OTHERS表示除了声明外的任意错误。SQLERRM是系统内置变量保存了当前错误的详细信息。

EXCEPTION

  WHEN OTHERS THEN
 
    vs_msg := 'ERROR IN user_learn_stat_p():' || SUBSTR(SQLERRM, 1, 500);
 
    ROLLBACK;
 
    --把当前错误记录进日志表。
 
    INSERT INTO LOG_INFO
   
      (proc_name, error_info, op_date)
   
    VALUES
   
      ('user_learn_stat_p', vs_msg, SYSDATE);
 
    COMMIT;
 
    RETURN;
 
END;
原创粉丝点击