I have a problem and a can't find a solution to it. I started working with PL/SQL in the past week, which means that my abilities are very limited.
Anyways, I had a working script that formatted a table to be export to another:
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;
however it needed a change to search only the last ID (S_ID) grouped by the NUMBER (N_PROT). So, I did another separeted script to search from the table the last ID (S_ID) group by the NUMBER (N_PROT):
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;
and the problem is how I will join this two scripts. One possible way to solve that came into my mind was to use execute the first script inside the loop in the second script (¹). Something like this:
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;
Thus, in this way, how would I save the insider SELECTs and export the results. I know that is not a good practice, but I couldn't come up with something better. If someone could give me some other perspective on how to solve this or help me with this solution that I came up with, it'd be great!
Just to resume the problem, I had a script already up and running and I needed to change it to do the same thing but add a condition that had to be only with the LAST IDs. I made a script that was able to get the IDs and isolate them with cursor, and now, I have to incorporate this IDs inside the first script.
Thank you!