Oracle
 sql >> Database >  >> RDS >> Oracle

C'è un modo per creare una tabella e alcune partizioni iniziali in modo dinamico?

Puoi provare con un po' di SQL dinamico, costruendo un'istruzione SQL basata su sysdate:

declare
    vSQL            varchar2(32767);
    vPartitions     varchar2(32767);
begin
    select listagg('PARTITION TABLE_NAME_' || to_char(sysdate + level/24, 'yyyymmddhh24') ||
                   ' VALUES LESS THAN (TIMESTAMP''' || to_char(sysdate + (level+1)/24, 'yyyy-mm-dd hh24') || ':00:00'')'
                   , ', ') within group (order by level) 
    into vPartitions
    from dual
    connect by level <= 24;
    --
    vSQL := q'[ CREATE TABLE TABLE_NAME
                (
                  CODE1             NUMBER(9)           DEFAULT ( 0 ),
                  CODE2             NUMBER(9)           DEFAULT ( 0 ),
                  CODE3             VARCHAR2(50 BYTE)   DEFAULT ( ' ' ),
                  VELOCITY          NUMBER(10,3)        DEFAULT ( 0 ),
                  REALDATE          TIMESTAMP(6),
                  LOCATION          NUMBER(7,3)         DEFAULT ( 0 ),
                  VALIDLOCATION     NUMBER(1)           DEFAULT ( 0 ),
                  STARTTIME         NUMBER(9)           DEFAULT ( 0 ),
                  OUTBOUND          NUMBER(1)           DEFAULT ( 0 ),
                  SERVICE_NAME      VARCHAR2(20 BYTE)   DEFAULT ( ' ' ),
                  LOCATIONCODE      NUMBER(3)           DEFAULT 0,
                  STARTDATE         TIMESTAMP(6),
                  CODE4             VARCHAR2(1 BYTE)
                )
                NOCOMPRESS 
                TABLESPACE TABLESPACE_NAME
                PCTUSED    40
                PCTFREE    10
                INITRANS   1
                MAXTRANS   255
                STORAGE    (
                            BUFFER_POOL      DEFAULT
                           )
                PARTITION BY RANGE (REALDATE)
                (]' || vPartitions ||
                ' )
                NOCACHE
                NOPARALLEL
                MONITORING';    
    execute immediate 'DROP TABLE TABLE_NAME CASCADE CONSTRAINTS'; 
    execute immediate vSQL;
end;