Sample Header Ad - 728x90

Need a PL/SQL code to drop many partition in 1 single SQL statements

1 vote
1 answer
816 views
I have a requirement where we need to drop historical partitions which are not dropped . Requirement is dropping multiple partitions in one step instead of dropping one by one. like ALTER TABLE sales DROP PARTITION sales_q1_2008, sales_q2_2008, sales_q3_2008, sales_q4_2008 update global indexes; instead of ALTER TABLE sales DROP PARTITION sales_q1_2008 update global indexes; ALTER TABLE sales DROP PARTITION sales_q2_2008 update global indexes; ALTER TABLE sales DROP PARTITION sales_q3_2008 update global indexes; and so on... app team has this code:- CREATE OR REPLACE PROCEDURE Test_user.DROP_PARTITIONS (p_table_name in varchar2, p_partition_key in number) is pragma autonomous_transaction; begin for cur in ( select partition_name from user_tab_partitions where table_name = p_table_name and substr(partition_name,1,8) <= 'P_'||trim(to_char(p_partition_key,'000000')) order by partition_position desc ) loop -- wait for processes which might have a lock on the partition to drop (max 10min) execute immediate 'lock table '||p_table_name||' partition ('||cur.partition_name||') in exclusive mode wait 600'; execute immediate 'alter table '||p_table_name||' drop partition '||cur.partition_name || ' UPDATE GLOBAL INDEXES'; end loop; end; / But I guess this is doing serial drop for each partitions thereby creating many alter statements instead of 1 step command where all the necessary partitions are dropped. How can we adapt this ?
Asked by Dhiren Singh (13 rep)
Mar 23, 2023, 03:08 PM
Last activity: Mar 23, 2023, 09:04 PM