Increase all the tablespace scrip in Oracle Database

Increase all the tablespace script in Oracle Database

Tablespace increase script 

set linesize 190


WITH low_space_ts AS (
    SELECT df.tablespace_name
    FROM (
        SELECT tablespace_name,
               SUM(bytes) totalspace
        FROM dba_data_files
        GROUP BY tablespace_name
    ) df
    LEFT JOIN (
        SELECT tablespace_name,
               SUM(bytes) freespace
        FROM dba_free_space
        GROUP BY tablespace_name
    ) fs
    ON df.tablespace_name = fs.tablespace_name
    WHERE ROUND(NVL(fs.freespace,0) / df.totalspace * 100, 2) <= 10
),
ts_files AS (
    SELECT tablespace_name,
           file_name,
           NVL(
               TO_NUMBER(
                   REGEXP_SUBSTR(
                       LOWER(file_name),
                       '_([0-9]+)\.dbf$',
                       1,
                       1,
                       NULL,
                       1
                   )
               ),
               1
           ) file_no,
           REGEXP_REPLACE(file_name,'[^/]+$','') path_name
    FROM dba_data_files
)
SELECT
    'ALTER TABLESPACE ' || t.tablespace_name ||
    ' ADD DATAFILE ''' ||
    MAX(t.path_name) ||
    LOWER(t.tablespace_name) || '_' ||
    LPAD(MAX(t.file_no) + 1, 2, '0') ||
    '.dbf'' SIZE 500M AUTOEXTEND ON NEXT 500M MAXSIZE 8G;' AS ddl
FROM ts_files t
JOIN low_space_ts l
    ON t.tablespace_name = l.tablespace_name
GROUP BY t.tablespace_name
ORDER BY t.tablespace_name;

Comments