create or replace procedure LRU2(p_val varchar2) as
MaxProcess constant pls_Integer :=5;
ExistMatch pls_Integer;
RecordCount pls_Integer;
begin
select max(case when PName = p_val then 1 else 0 end),count(*)
into ExistMatch,RecordCount
from LRUTable2;
if ExistMatch = 0 and RecordCount = MaxProcess then
update LRUTable2
set PID = decode(PID-1,0,MaxProcess,PID-1),
PName = decode(PID,1,p_val,PName);
elsif RecordCount=0 or ExistMatch = 0 and RecordCount < MaxProcess then
insert into LRUTable2(PID,PName) values(RecordCount+1,p_val);
else --ExistMatch = 1
update LRUTable2 a
set PID = (select b.NewPID
from (select bb.RowID as Row_ID,
Row_Number() over(order by decode(bb.PName,p_val,1,0),bb.PID) as NewPID
from LRUTable2 bb) b
where a.RowID = b.Row_ID);
end if;
end;
/
sho err
truncate table LRUTable2;
exec LRU2('NotePad');
exec LRU2('EmEditor');
exec LRU2('Oracle');
exec LRU2('iexplore');
select PID,PName from LRUTable2 order by PID;
exec LRU2('Java');
select PID,PName from LRUTable2 order by PID;
exec LRU2('iexplore');
select PID,PName from LRUTable2 order by PID;
exec LRU2('qprocess');
select PID,PName from LRUTable2 order by PID;