ORA-01735:使用动态SQL在段上释放未使用的空间时,无效的ALTER TABLE选项

时间:2017-08-19 19:52:35

标签: oracle stored-procedures plsql dynamic-sql plsqldeveloper

我开发了一个简单的打包过程,允许段(HWM上方)不使用的空闲空间。它接收模式名称,表名称和 KEEP claus e的值作为参数。

当我使用 EXECUTE IMMEDIATE 语句运行DDL语句时,它告诉我 ALTER选项无效。如果我从普通终端运行相同的语句,这可以正常工作。

BLOQUES ASIGNADOS -> 8
BLOQUES OCUPADOS -> 5
TAMAÑO INICIAL -> 9
LIBERANDO ESPACIO ALTER TABLE SERGIO11.EMPLOYEES DEALLOCATE UNUSED;
ERROR : -1735MENSAJE: ORA-01735: opción ALTER TABLE no válida

以下是程序的定义:

PROCEDURE RELEASE_UNUSED_SPACE(
            p_owner IN DBA_TABLES.OWNER%TYPE,
            p_table IN DBA_TABLES.TABLE_NAME%TYPE,
            p_keep IN NUMBER DEFAULT NULL,
            p_unit IN VARCHAR2 DEFAULT 'M'
        ) AS
            UNAVAILABLE_UNIT EXCEPTION;
            TYPE R_SEGMENT_BLOCKS_INFO IS RECORD (
                occupied_blocks   DBA_SEGMENTS.BLOCKS%TYPE,
                assigned_blocks   DBA_SEGMENTS.BLOCKS%TYPE,
                initial_extent    DBA_SEGMENTS.INITIAL_EXTENT%TYPE
            );

            v_segmentBlocksInfo R_SEGMENT_BLOCKS_INFO;
            v_deallocate_sql VARCHAR2(100);

        BEGIN

            IF(p_owner IS NULL OR p_table IS NULL) THEN
                RAISE INVALID_ARGUMENTS;
            END IF;

            IF (p_keep IS NOT NULL AND UPPER(p_unit) NOT IN ('M', 'K')) THEN
                RAISE UNAVAILABLE_UNIT;
            END IF;

            SELECT T.BLOCKS, S.BLOCKS, CEIL(S.INITIAL_EXTENT / 8000)
            INTO v_segmentBlocksInfo
            FROM DBA_TABLES T, DBA_SEGMENTS S
            WHERE S.SEGMENT_NAME = T.TABLE_NAME AND S.OWNER = T.OWNER
                AND S.OWNER = T.OWNER
                AND T.TABLE_NAME = UPPER(p_table) AND T.OWNER = UPPER(p_owner);

            PRINT_LINE('BLOQUES ASIGNADOS -> ' || v_segmentBlocksInfo.assigned_blocks);
            PRINT_LINE('BLOQUES OCUPADOS -> ' || v_segmentBlocksInfo.occupied_blocks);
            PRINT_LINE('TAMAÑO INICIAL -> ' || v_segmentBlocksInfo.initial_extent);

            IF v_segmentBlocksInfo.occupied_blocks < v_segmentBlocksInfo.assigned_blocks THEN
                v_deallocate_sql := 'ALTER TABLE ' || p_owner || '.' || p_table || ' DEALLOCATE UNUSED';
                IF p_keep IS NOT NULL THEN
                    v_deallocate_sql := v_deallocate_sql || ' KEEP ' || p_keep || 'M'; 
                END IF;
                v_deallocate_sql := v_deallocate_sql || ';';
                PRINT_LINE('LIBERANDO ESPACIO ' || v_deallocate_sql);
                EXECUTE IMMEDIATE v_deallocate_sql;
            END IF;

        EXCEPTION
            WHEN NO_DATA_FOUND THEN
                PRINT_LINE('TABLE ' || p_table || ' ON SCHEMA ' || p_owner || ' NOT EXISTS');
            WHEN UNAVAILABLE_UNIT THEN
                PRINT_LINE('UNIT OF MEASURE NOT VALID. Only M or K is allowed');
            WHEN OTHERS THEN
                PRINT_LINE('ERROR : ' || SQLCODE || 'MENSAJE: ' || SQLERRM);
        END RELEASE_UNUSED_SPACE;

例如,当使用此参数执行过程时,结果句子将为:

EXEC MANAGEMENT_OF_TABLES_AND_INDEX.RELEASE_UNUSED_SPACE('SERGIO11', 'EMPLOYEES');

ALTER TABLE SERGIO11.EMPLOYEES DEALLOCATE UNUSED;

我已经向所有者用户明确授予了ALTER ANY TABLE权限,该用户也是执行它的用户。

有谁知道问题所在。在此先感谢!!!。

1 个答案:

答案 0 :(得分:2)

尝试删除此声明

v_deallocate_sql := v_deallocate_sql || ';';

所以你的判决应该是:

ALTER TABLE SERGIO11.EMPLOYEES DEALLOCATE UNUSED