Oracle Syntax

I have a table with year/quarter partitions like '2010Q1, 2010Q2....'

I want to truncate all Q2 partitions in this table across all years.

I know I can do it by "alter table <table_name> truncate partition 2010Q2, 2011Q2, 2012Q2 etc.

But, I want to write a dynamic sql like:

alter table <table_name> truncate partitions in (SELECT partition_name FROM ALL_TAB_PARTITIONS
where table_owner = 'abcd'
and table_name = 'bcdef'
and partition_name like '%Q2'


I know the above query doesnt work, but is there a way to write this kind of query?


Thanks
MRPTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flow01Commented:
In a 2 step approach :
create the sql
execute it

You can combine them in an oracle procedure

create or replace
procedure truncate_qn (p_table_name varchar2, p_qn varchar2)
is
  v_statement varchar2(32767);
begin
  for r1 in (
SELECT partition_name FROM ALL_TAB_PARTITIONS
where table_owner = 'abcd'
and table_name = 'bcdef'
and partition_name like '%' || p_qn
) loop
  if v_statement is not null then
     v_statement := v_statement || ',';
  end if;
  v_statement := v_statement || r1.partition_name;
end loop;

  v_statement  :=  'alter table ' || p_table_name || ' truncate partitions in ' ||
                 v_statement;
 
  dbms_output.put_line(v_statement);
  --execute immediate v_statement;  -- uncomment if you see the right statement
end;
/
0
awking00Information Technology SpecialistCommented:
declare
v_sql varchar2(255);
begin
for p in
(select partition_name
 from all_tab_partitions
 where table_owner = 'ABCD'
 and table_name = 'BCDEF'
 and partition_name like '%Q2')
loop
v_sql := 'alter table abcd.bcdef truncate partition '||p.partition_name;
execute immediate v_sql;
end loop;
end;
/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MRPTAuthor Commented:
Thanks a lot.
0
awking00Information Technology SpecialistCommented:
You're welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.