I have a collection of sysid’s that I am using to iterate over in several forall loops. This script is meant to be run on a regular basis, so I would like to know whether the collection persists in the database and needs to be cleared out, or if the script is alright as-is.
Also, I am new to PL/SQL, so if you see anything wrong with the script, please do let me know.
This is going to run on Oracle 10g and 11g.
Thanks
DECLARE
TYPE sSysid IS TABLE OF person.sysid%TYPE
INDEX BY PLS_INTEGER;
l_sSysid sSysid;
BEGIN
SELECT sysid
BULK COLLECT INTO l_sSysid
FROM person
where purge_in_process = 1;
FORALL i IN l_sSysid.FIRST .. l_sSysid.LAST
delete from person_attribute where property_pk like concat(l_sSysid(i), '%');
FORALL i IN l_sSysid.FIRST .. l_sSysid.LAST
delete from person_property where person_sysid = l_sSysid(i);
FORALL i IN l_sSysid.FIRST .. l_sSysid.LAST
delete from person where sysid = l_sSysid(i);
END;
/
commit;
The collection is a local variable so it will no longer exist after the block finishes executing. There will be no need to clear it out. Depending on the number of rows in the PERSON table where PURGE_IN_PROCESS will be 1, you may want to use the LIMIT clause in order to avoid consuming too much PGA memory though.
The idea of an anonymous PL/SQL block that is run regularly, however, is a bit foreign to me. If you intend the code to be run regularly, I’d strongly suggest that you create a stored procedure rather than an anonymous block and then schedule the procedure to be run regularly. That opens up the ability to use the database’s scheduling facilities (DBMS_JOB and DBMS_SCHEDULER) to run the process and allows other applications to call it as well if the need ever arises. Plus, you’ll get the benefits of things like dependency tracking in the database.