Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get names of partition in oracle while i input a date

I have a table with many partitions range. I need to get the name of all partition when I give a date. For eg: if I input date 20/09/2014, it should list all partitions before that given date.

create or replace function get_part_name(p_date in date)
return varchar2 is
d date;
retp varchar2(30);
mind date:=to_date('4444-01-01','yyyy-mm-dd');
str varchar2(32000);
cursor c is
select high_value, partition_name p
  from user_tab_partitions
 where table_name='TEST';
begin
  for r in c loop
     str := r.high_value;
     execute immediate 'select '||str||' from dual' into d;     
     if p_date<d and d<mind then
        retp:=r.p;
        mind:=d;
     end if;
  end loop;
  return retp;
end;

This is returing a single date. I need all the dates, is it possible?

like image 544
Charles Peter Avatar asked Oct 16 '14 07:10

Charles Peter


People also ask

How do I find the partition name in Oracle?

declare l_data long; begin select high_value into l_data FROM DBA_TAB_PARTITIONS WHERE TABLE_OWNER='SCOTT' AND PARTITION_NAME = 'EMP_P0'; dbms_output.

How do I fetch records for a specific date in Oracle?

SELECT * FROM SDE. fuse_h WHERE DATECREATED between to_date('18/01/2002','DD/MM/YYYY') and to_date('18/01/2002 23:59:59','DD/MM/YYYY HH24:MI:SS'); this will allow any index to be used that exists on the datecreated column too. Remember, always use 4 digit years when specifying dates.

How can I get partition details of a table?

You can use the sys. partitions system catalog view to return partition info for a table and most kinds of views. You can use the sys. dm_db_partition_stats system dynamic management view to return page and row-count information for every partition in the current database.

Can you partition by date in SQL?

You can partition on DateCreated column after you make it a clustered index.


2 Answers

WITH DATA AS (
select table_name,
       partition_name,
       to_date (
          trim (
          '''' from regexp_substr (
                     extractvalue (
                       dbms_xmlgen.getxmltype (
                       'select high_value from all_tab_partitions where table_name='''
                                || table_name
                                || ''' and table_owner = '''
                                || table_owner
                                || ''' and partition_name = '''
                                || partition_name
                                || ''''),
                             '//text()'),
                          '''.*?''')),
          'syyyy-mm-dd hh24:mi:ss')
          high_value_in_date_format
  FROM all_tab_partitions
 WHERE table_name = 'SALES' AND table_owner = 'SH'
 )
 SELECT * FROM DATA
   WHERE high_value_in_date_format < SYSDATE
/

TABLE_NAME           PARTITION_NAME       HIGH_VALU
-------------------- -------------------- ---------
SALES                SALES_Q4_2003        01-JAN-04
SALES                SALES_Q4_2002        01-JAN-03
SALES                SALES_Q4_2001        01-JAN-02
SALES                SALES_Q4_2000        01-JAN-01
SALES                SALES_Q4_1999        01-JAN-00
SALES                SALES_Q4_1998        01-JAN-99
SALES                SALES_Q3_2003        01-OCT-03
SALES                SALES_Q3_2002        01-OCT-02
SALES                SALES_Q3_2001        01-OCT-01
SALES                SALES_Q3_2000        01-OCT-00
SALES                SALES_Q3_1999        01-OCT-99
SALES                SALES_Q3_1998        01-OCT-98
SALES                SALES_Q2_2003        01-JUL-03
SALES                SALES_Q2_2002        01-JUL-02
SALES                SALES_Q2_2001        01-JUL-01
SALES                SALES_Q2_2000        01-JUL-00
SALES                SALES_Q2_1999        01-JUL-99
SALES                SALES_Q2_1998        01-JUL-98
SALES                SALES_Q1_2003        01-APR-03
SALES                SALES_Q1_2002        01-APR-02
SALES                SALES_Q1_2001        01-APR-01
SALES                SALES_Q1_2000        01-APR-00
SALES                SALES_Q1_1999        01-APR-99
SALES                SALES_Q1_1998        01-APR-98
SALES                SALES_H2_1997        01-JAN-98
SALES                SALES_H1_1997        01-JUL-97
SALES                SALES_1996           01-JAN-97
SALES                SALES_1995           01-JAN-96

28 rows selected.

SQL>

Use your desired date in place of SYSDATE in above query. Or you can pass it as INPUT through the FUNCTION and RETURN the result set.

like image 85
Lalit Kumar B Avatar answered Oct 08 '22 05:10

Lalit Kumar B


Find Partition name using date IF you have a meaningful date column in the table in Oracle DB

WITH table_sample AS (select COLUMN_WITH_DATE from table SAMPLE (5))

SELECT uo.SUBOBJECT_NAME AS "PARTITION_NAME_1" FROM table_sample sw,
SYS.USER_OBJECTS uo
WHERE sw.COLUMN_WITH_DATE = TRUNC(SYSDATE) -- ENTER DATE HERE AS 'DD-MM-YYYY 00:00:00'
AND OBJECT_ID = dbms_rowid.rowid_object(sw.rowid)
AND ROWNUM < 2;

like image 29
gidi gob Avatar answered Oct 08 '22 05:10

gidi gob