Код:
declare
vCount integer;
vDate date := trunc(sysdate)-180;
begin
loop
vCount := 0;
for vRec in (select D.ID, D.ClientIndex, D.Location, O.DatedAt
from SMDocuments D, SMDateDocs O
where D.DocType = 'OR'
and D.Createdat >= vDate
and D.DocState >= 2
and O.DocType = 'OR'
and O.ID = D.ID
and O.DatedAt is not null
and not exists (select 1 from FSLastOR F
where F.DocType = 'OR' and F.DocID=D.ID)
and rownum<100
)
loop
insert into FSLastOR(DocType, DocID, SpecItem, Article, ClientIndex, Location, OrderDate)
select S.DocType, S.DocID, S.SpecItem, S.Article,
vRec.ClientIndex, vRec.Location, vRec.DatedAt
from SMSpec S
where S.DocType = 'OR' and S.DocID = vRec.ID;
vCount := vCount + 1;
end loop;
commit;
exit when vCount = 0;
end loop;
end;
/
analyze table FSLastOR compute statistics for all columns;