I have a code which accepts a table name as parameter and creates subpartitions on that table name for the partitions. My table is partitioned on list of source system codes and range subpartitoned on monthly basis.
Question: If suppose I have variable list of tables and I want to create subpartitions for all then I need to modify this procedure every time to pass varying list of tables. Can I use PLSQL VARRAY or nested table to hold my list of tables, and pass this VARRAY or nested table as a parameter to the below procedure and create subpartitions for all the table names the VARRAY or nested table is holding?
Your help is much appreciated. Many thanks!
Code:
CREATE OR REPLACE PROCEDURE execute_subpartition ( table_name IN varchar2)
IS
tbl_nm varchar2(30) := table_name;
sqlstr VARCHAR2(1000);
CURSOR TabSubPartition IS
SELECT TABLE_NAME, PARTITION_NAME
FROM USER_TAB_PARTITIONS
WHERE TABLE_NAME = tbl_nm
ORDER BY PARTITION_NAME;
BEGIN
FOR aSubPart IN TabSubPartition LOOP
IF TRUNC(LAST_DAY(SYSDATE)) = TRUNC(SYSDATE)
sqlstr := 'ALTER TABLE TUCEL001.' || aSubPart.TABLE_NAME || ' MODIFY PARTITION ' ||
aSubPart.PARTITION_NAME ||' ADD SUBPARTITION ' || aSubPart.PARTITION_NAME || '_' ||
TO_CHAR(TRUNC(LAST_DAY(SYSDATE) + 1), 'MON_YYYY') ||' VALUES LESS THAN (TIMESTAMP ''' ||
TRIM(to_char(add_months((TRUNC(LAST_DAY(SYSDATE))+1), 1), 'SYYYY-MM-DD HH24:MI:SS',
'NLS_CALENDAR=GREGORIAN')) || ''')';
dbms_output.put_line(sqlstr);
EXECUTE IMMEDIATE sqlstr;
ELSE
dbms_output.put_line('the condition did not match');
END IF;
END LOOP;
Exception
WHEN OTHERS
THEN
dbms_output.put_line('encountered an error, because the sub-partitions which are being created
already exists');
END;