在 Oracle 中将过程更改为视图

Posted

技术标签:

【中文标题】在 Oracle 中将过程更改为视图【英文标题】:Changing a Procedure to a View in Oracle 【发布时间】:2014-11-10 20:58:33 【问题描述】:

不确定这是否可行,但我正在尝试查看是否可以将此过程转换为视图,因为在运行过程时驱动器无法填充表格时遇到了问题。

我正在尝试理解其他人的代码,但由于光标的原因,我什至不确定我们是否可以将此过程更改为视图。

----------------------------------------------------------------------
--This Procedure will interface drive information on a nightly basis--
----------------------------------------------------------------------
Procedure HEMA_DRIVE_AUTO IS

v_start_date               DATE     := trunc(sysdate) -30;      
v_end_date                 DATE     := trunc(sysdate);          
v_delete_stats_dt          DATE     := trunc(sysdate)-120;

v_total_registration_count NUMBER;
v_total_performed_count    NUMBER;
v_total_collected_count    NUMBER;
v_total_deferred_count     NUMBER;
v_total_qns_count          NUMBER;
v_existing_drive           NUMBER;
v_existing_performed       NUMBER;
v_maph_drive               NUMBER;

--This Cursor will collect the initial data
cursor c_drive_info is
  select dr.drive_id, dr.Start_time, dr.vehicle_id
    from drives dr
   --where dr.drive_id in(1605606);
   where trunc(dr.start_time) between v_start_date and v_end_date;

--This Cursor will be used to decode the Donation Types
cursor c_procedure_codes is
  select * from hema_donation_type_map hdt 
    where hdt.mobiles = 1 order by procedure_code_id;

--This Cursor will define the intentions but exclude theraputics inthe mapping
cursor c_intention is
  select rsa_motivation_id,hema_intent_id from hema_intent_map 
    where rsa_motivation_id <> 4 order by rsa_motivation_id;

   BEGIN
-- delete records older then 4 months
delete from hema_nightly h where trunc(h.drive_date) < v_delete_stats_dt;
commit;

FOR cur_drive IN c_drive_info LOOP

 delete from hema_nightly where drive_id = cur_drive.drive_id;
 commit;

 -- Loop by motivation/intention
 FOR cur_intent in c_intention LOOP

  -- Loop to get the procedure code data
  FOR cur_proc_code IN c_procedure_codes LOOP

    v_total_registration_count := 0;
    v_total_performed_count    := 0;
    v_total_collected_count    := 0;
    v_total_deferred_count     := 0;
    v_total_qns_count          := 0;
    v_maph_drive               := 0;


      -- get the count for all other procedures
      select count(1)
        into v_total_registration_count
        from registration r
       where r.drive_id = cur_drive.drive_id
         and r.donation_type_id = cur_proc_code.donation_type_id
         and r.motivation_id = cur_intent.rsa_motivation_id;

      --get the deferral count
      select count(unique(r.registration_id))
        into v_total_deferred_count
        from registration r
       where r.drive_id = cur_drive.drive_id
         and r.donation_type_id = cur_proc_code.donation_type_id
         and r.motivation_id = cur_intent.rsa_motivation_id
         and r.step_completed < 12
         and exists (select rsc.registration_id
                from reg_steps_completed rsc
               where rsc.registration_id = r.registration_id
                 and rsc.collection_step_id = 99);

      -- QNS count
      select count(unique(r.registration_id))
        into v_total_qns_count
        from registration r
       where r.drive_id = cur_drive.drive_id
         and r.step_completed < 12
         and not exists (select rsc.registration_id
                from reg_steps_completed rsc
               where rsc.registration_id = r.registration_id
                 and rsc.collection_step_id = 99)
         and r.donation_type_id = cur_proc_code.donation_type_id
         and r.motivation_id = cur_intent.rsa_motivation_id;

    -- performed count is the difference between total registrations and total deferrals.
    v_total_performed_count := v_total_registration_count -
                               (v_total_deferred_count +
                               v_total_qns_count);

    -- not calulatind yield so keep count the same
    v_total_collected_count := v_total_performed_count; 

    -- does this drive exist 
    select count(drive_id)
      into v_existing_drive
      from hema_nightly
     where drive_id = cur_drive.drive_id
       and procedure_id = cur_proc_code.procedure_code_id
       and intent = cur_intent.hema_intent_id;

    -- Is this an aph vehicle?
     select count(vehicle_id)
       into v_maph_drive
       from vehicles
      where veh_drive_type_uid = 2
        and vehicle_id = cur_drive.vehicle_id;

    if v_existing_drive > 0 then

      update hema_nightly
           set performed  = performed  + v_total_performed_count,
               collected  = collected  + v_total_collected_count,
               registered = registered + v_total_registration_count,
               deferrals  = deferrals  + v_total_deferred_count,
               qns        = qns        + v_total_qns_count,
               drive_date = cur_drive.start_time,
               mod_date   = sysdate,
               intent     = cur_intent.hema_intent_id,
               aph        = v_maph_drive
         where drive_id = cur_drive.drive_id
           and procedure_id = cur_proc_code.procedure_code_id
           and intent = cur_intent.hema_intent_id;
        commit;

     elsif v_existing_drive = 0 and v_total_registration_count > 0 then
      insert into hema_nightly
        (drive_id,
         procedure_id,
         performed,
         collected,
         registered,
         deferrals,
         qns,
         drive_date,
         mod_date,
         intent,
         aph)
      values
        (cur_drive.drive_id,
         cur_proc_code.procedure_code_id,
         v_total_performed_count,
         v_total_collected_count,
         v_total_registration_count,
         v_total_deferred_count,
         v_total_qns_count,
         trunc(cur_drive.start_time),
         sysdate,
         cur_intent.hema_intent_id,
         v_maph_drive);
      commit;
     end if;

    v_existing_drive := 0;

  end loop;
 end loop;
end loop;

end hema_drive_auto;

【问题讨论】:

【参考方案1】:

视图不执行 DML(插入、更新、删除),也不使用 COMMIT 和 ROLLBACK 管理事务;他们只选择和检索数据。

【讨论】:

以上是关于在 Oracle 中将过程更改为视图的主要内容,如果未能解决你的问题,请参考以下文章

在 Oracle 中更新列值的过程

如何在升级过程中运行捆绑包时,如何在WIX(Windows安装程序xml)引导程序项目中将按钮文本更改为“升级”?

如何在颤动中将格式从列表视图更改为网格视图?

iOS 7:如何在一个视图控制器中将状态栏文本颜色更改为白色,在第二个视图控制器中更改为黑色?

如何在TheSchwartz中将数据库从MySQL更改为Oracle

在vim中将一对括号更改为括号的最快方法