我遇到了问题,无法找到解决方法。我在过去一周开始使用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)。所以,我做了另一个separeted脚本,用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并导出结果。我知道这不是一个好习惯,但我无法想出更好的东西。 如果有人能给我一些关于如何解决这个问题或者帮助我解决这个问题的其他观点,那就太棒了!
只是为了恢复问题,我已经启动并运行了一个脚本,我需要更改它以执行相同的操作,但添加一个必须只有最后ID的条件。我创建了一个能够获取ID并使用游标隔离它们的脚本,现在,我必须将这些ID合并到第一个脚本中。
谢谢!
答案 0 :(得分:1)
如果您想将数据从一个表“导出”到另一个表并且您有选择,为什么不尝试使用“insert as select”?
insert into your_new_table select <your_data> from N_TRANS where ...
答案 1 :(得分:1)
JobLauncher jobLauncher = context.getBean(JobLauncher.class);
MyXmlJobConfig config = getConfigFromFile(); // Loading config from file
MyCustomJobBuilder myCustomJobBuilder = context.getBean(MyCustomJobBuilder.class);
Job createdJob = myCustomJobBuilder.createJob(config);
jobLauncher.run(createdJob, new JobParameters());
答案 2 :(得分:0)
看起来你只需要这样的东西:
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