繁体   English   中英

如何使用两个光标并显示它们PL / SQL

[英]How to use two cursors and display them PL/SQL

我有一个问题,找不到解决方案。 过去一周,我开始使用PL / SQL,这意味着我的能力非常有限。

无论如何,我有一个工作脚本来格式化要导出到另一个表的表:

    select
        '7' as M_ID,
         N_PROT as SUBSCRIPTION_VERSION_ID,
      /* ..... bunch of other business logic */
    from N_TRANS
    where (CORA in ('0215','0320') or CORA in ('0215', '0320'))
    and N_PROT IS NOT null;

但是,它需要更改以仅搜索按NUMBER(N_PROT)分组的最后一个ID(S_ID)。 因此,我执行了另一个分离的脚本,以NUMBER(N_PROT)从表中搜索最后一个ID(S_ID)组:

    declare
       cursor S_CURS IS
          select 
               max(S_ID) keep(dense_rank LAST order by N_PROT) S_ID,
               NR_PROTOCOLO
          from N_TRANS 
          group by N_PROT
          order by S_ID;

    S_HIST_T  S_CURS%ROWTYPE;
    type NTT_S_HIST_TRANS is table of S_HIST_T%type;
    LLL_S_HIST_TRANS NTT_S_HIST_TRANS;

    begin
       open  S_CURS;
       fetch S_CURS bulk collect into LLL_S_HIST_TRANS;
       close S_CURS;
       for indxx in 1..LLL_S_HIST_TRANS.count loop
          --The IDs as a var
          DBMS_OUTPUT.PUT_LINE(LLL_S_HIST_TRANS(indxx).S_HIST_T);
       end loop;
   end;

问题是我将如何加入这两个脚本。 解决这个问题的一种可能方法是在第二个脚本(¹)中使用在循环内执行第一个脚本 像这样:

    declare
       cursor S_CURS IS
          select 
          ...     
    begin
      ... 
      for indxx in 1..LLL_S_HIST_TRANS.count loop
          --The IDs as a var
          select
             '7' as M_ID,
              N_PROT as SUBSCRIPTION_VERSION_ID,
              /* ..... bunch of other business logic */
          from N_TRANS
          where (CORA in ('0215','0320') or CORA in ('0215', '0320'))
          and N_PROT IS NOT null
          and S_ID = LLL_S_HIST_TRANS(indxx).S_HIST_T; -- (¹)
       end loop;
   end;

因此,以这种方式,我将如何保存内部SELECT并导出结果。 我知道这不是一个好习惯,但是我无法提出更好的建议。 如果有人可以给我其他解决方案的观点,或者为我提出的解决方案提供帮助,那就太好了!

为了解决这个问题,我已经启动并运行了一个脚本,我需要对其进行更改以执行相同的操作,但添加的条件只能是LAST ID。 我制作了一个脚本,该脚本能够获取ID并使用光标将其隔离,现在,我必须将此ID合并到第一个脚本中。

谢谢!

如果您想将数据从一个表“导出”到另一个表并且具有选择权,为什么不尝试使用“作为选择插入”呢?

insert into your_new_table select <your_data> from N_TRANS where ...
begin
  for cv1 in (select
                  max(S_ID) keep(dense_rank LAST order by N_PROT) S_ID,              
                  NR_PROTOCOLO
                 from N_TRANS 
                 group by N_PROT
                 order by S_ID)
  loop
    for cv2 in (select
                '7' as M_ID,
                 N_PROT as SUBSCRIPTION_VERSION_ID,
                 /* ..... bunch of other business logic */
              from N_TRANS
              where (CORA in ('0215','0320') or CORA in ('0215', '0320'))
                and N_PROT IS NOT null
                and S_ID = cv1.S_ID)
    loop
      -- Processing logic here. To access either loop's data,
      -- prefix the column with cv1 or cv2, like cv2.M_ID, cv1.NR_PROTOCOLO
    end loop;  
  end loop;
end;

看起来您只需要这样的东西:

select m_id, n_prot, etc
from   ( select '7' as m_id
              , n_prot as subscription_version_id
              , /* ..... bunch of other business logic */
              , row_number() over(partition by n_prot order by s_id desc) as seq
         from   n_trans
         where  ( cora in ('0215', '0320') or cora in ('0215', '0320') )
         and    n_prot is not null )
where  seq = 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM