Thursday, February 9, 2012

Syntax to add partitions dynamically

Dear Oracle experts,
In our system we have a requirement like...
we need to create a partitioned table with 3 partitions and each
partition contains data of only one day.
Moreover I need to store only the last 3 days data in those
partitions(need not bother about old data).
I mean, for eg. today is 30-apr-2011, i need to have 28/29/30-apr-2011
days data in the partitions of table.
Hope this is clear.

Please guide me the way to implement this.

Thanks.

Regards,
Madhu.

Best Answerby Larson E Apr 29, 2011
We do this by creating a table and partition on the date, so the partition
would be defined as:

alter table abc add partition abc_20110428 values less than ('29-APR-11');
alter table abc add partition abc_20110429 values less than ('30-APR-11');
alter table abc add partition abc_20110430 values less than ('01-MAY-11');

A job is created that runs each day. On this example, let's assume the job
runs at 1AM each nigh. So on May 2, data for 4/29 would be processed, then
remove the oldest partition, such as:

declare

my_date date;
my_datenum number;
my_partition varchar2(16);

my_date := to_char(sysdate-3);

do_processing (my_date);

-- remove old partition

my_partition := 'abc_' to_char(my_date, 'YYYYMMDD');

execute immediate 'alter table abc drop partition ' || my_partition || '
update global indexes';

-- now add my new partition for today

my_partition := 'abc_' || to_number(to_char(sysdate));

execute immediate 'alter table abc add partition ' || my_partition || '
values less than (''' || to_char(sysdate) || ''')';

-- schedule tomorrow's job

No comments: