Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

PLSQL rename automatic list PARTITIONs PLSQL syntax

PugzlyFeb 5 2022

I have a process that is renaming system GENERATED list PARTITIONs from SYS_P##### to high_value, which seems to be working fine.

I am struggling and can't seem to get the syntax correct to prefix the new partition_name with 'P_' in order to produce names such as P_IOWA, P_TEXAS…

FYI, since some of the partition values have spaces - like 'New York' I don't want to force users to double quote the PARTITION names when referencing them so I decided to replace the space with an underscore to create a name like NEW_YORK.

My goal is to have every new partition name to be prefixed with P_ yielding a name like P_NEW_YORK or P_IOWA

I've tried several combinations but haven't been successful. Any help would be greatly appreciated. Below is my test CASE and code. Thanks in advance to all who respond and your help and expertise.

CREATE TABLE T21    

(
seq_num NUMBER GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
num NUMBER(*,0),
state VARCHAR2(20)
)
SEGMENT CREATION DEFERRED
PARTITION BY LIST (state) AUTOMATIC
(PARTITION P_CALIFORNIA VALUES ('CALIFORNIA')
);

insert into t21 (num, state)
select
level * round(dbms_random.value(1,50)),
case round(dbms_random.value(1,50))
when 1 then 'Alabama'
when 2 then 'Alaska'
when 3 then 'Arizona'
when 4 then 'Arkansas'
when 5 then 'California'
when 6 then 'Colorado'
when 7 then 'Connecticut'
when 8 then 'Delaware'
when 9 then 'Florida'
when 10 then 'Georgia'
when 11 then 'Hawaii'
when 12 then 'Idaho'
when 13 then 'Illinois'
when 14 then 'Indiana'
when 15 then 'Iowa'
when 16 then 'Kansas'
when 17 then 'Kentucky'
when 18 then 'Louisiana'
when 19 then 'Maine'
when 20 then 'Maryland'
when 21 then 'Massachusetts'
when 22 then 'Michigan'
when 23 then 'Minnesota'
when 24 then 'Mississippi'
when 25 then 'Missouri'
when 26 then 'Montana'
when 27 then 'Nebraska'
when 28 then 'Nevada'
when 29 then 'New Hampshire'
when 30 then 'New Jersey'
when 31 then 'New Mexico'
when 32 then 'New York'
when 33 then 'North Carolina'
when 34 then 'North Dakota'
when 35 then 'Ohio'
when 36 then 'Oklahoma'
when 37 then 'Oregon'
when 38 then 'Pennsylvania'
when 39 then 'Rhode Island'
when 40 then 'South Carolina'
when 41 then 'South Dakota'
when 42 then 'Tennessee'
when 43 then 'Texas'
when 44 then 'Utah'
when 45 then 'Vermont'
when 46 then 'Virginia'
when 47 then 'Washington'
when 48 then 'West Virginia'
when 49 then 'Wisconsin'
when 50 then 'Wyoming'
end
from dual
connect by level <= 100;

SELECT TABLE_NAME, PARTITION_NAME, HIGH_VALUE FROM USER_TAB_PARTITIONS WHERE TABLE_NAME ='T21';

CREATE OR REPLACE PROCEDURE RenameListPartitions
IS

CURSOR PartTables IS  
SELECT TABLE\_NAME  
FROM USER\_PART\_TABLES   
WHERE PARTITIONING\_TYPE = 'LIST'   
ORDER BY TABLE\_NAME;  

CURSOR TabParts(aTableName VARCHAR2) IS   
SELECT PARTITION\_NAME, HIGH\_VALUE  
FROM USER\_TAB\_PARTITIONS  

WHERE regexp_like(partition_name,'^SYS_P[[:digit:]]{1,10}') AND
TABLE_NAME = aTableName AND
table_name not like 'BIN$%'
ORDER BY PARTITION_POSITION;

newPartName VARCHAR2(30);

BEGIN

FOR aTab IN PartTables LOOP   
     
    FOR aPart IN TabParts(aTab.TABLE\_NAME) LOOP      

execute immediate 'select ' || aPart.HIGH_VALUE || ' from dual' into newPartName;

        IF newPartName \<> aPart.PARTITION\_NAME THEN  
            EXECUTE IMMEDIATE 'ALTER TABLE '||aTab.TABLE\_NAME||' RENAME PARTITION '||aPart.PARTITION\_NAME||' TO '||upper(replace(newPartName,' ','\_'));  
        END IF;               
    END LOOP;  
END LOOP;  

END RenameListPartitions;
/

BEGIN
RenameListPartitions;
END;
/

Comments
Post Details
Added on Feb 5 2022
1 comment
590 views