PL / SQL匿名块与PL / SQL过程

时间:2014-11-11 20:55:19

标签: oracle stored-procedures plsql indexing

这是我在StackOverflow上的第一篇文章。我不是开发人员,但我希望能够解决一些已有10年历史的PL / SQL类笔记,并且可能会稍微增加我萎缩的PL / SQL技能。

我试图编写简单的PL / SQL过程来重建Oracle模式中的索引。

代码在作为PL / SQL匿名块编写和执行时工作正常,但在编写和执行PL / SQL过程时会抛出错误。

请查看并回答有关如何解决这个小小的益智游戏的任何问题或建议。

干杯, CB

--IDX_REBUILD: Anonymous Block
set timing on;
SET SERVEROUTPUT ON;
DECLARE
    CURSOR Index_Cur IS
        SELECT owner, index_name 
        FROM cjb_indexes 
        WHERE (owner like 'ZIPPY%')
        AND INDEX_TYPE = 'NORMAL' 
        ORDER BY owner, index_name;
    SQL_STMT VARCHAR2(200);
BEGIN
    DBMS_OUTPUT.ENABLE (100000);
    FOR IndexRec IN Index_Cur LOOP
        SQL_STMT := ('ALTER INDEX ' || IndexRec.owner || '.' || IndexRec.index_name || ' REBUILD');
        DBMS_OUTPUT.PUT_LINE(SQL_STMT);
        EXECUTE IMMEDIATE SQL_STMT;
        DBMS_OUTPUT.NEW_LINE;
    END LOOP;
END;
/

....
ALTER INDEX ZIPPY.R157_SDE_ROWID_UK REBUILD
ALTER INDEX ZIPPY.R49_SDE_ROWID_UK REBUILD
ALTER INDEX ZIPPY.R50_SDE_ROWID_UK REBUILD
ALTER INDEX ZIPPY.R51_SDE_ROWID_UK REBUILD
ALTER INDEX ZIPPY.R52_SDE_ROWID_UK REBUILD


PL/SQL procedure successfully completed.


--IDX_REBUILD:  PROCEDURE
set timing on;
SET SERVEROUTPUT ON;
CREATE OR REPLACE PROCEDURE CJB_IDX_REBUILD
IS
    CURSOR Index_Cur IS
        SELECT owner, index_name 
        FROM cjb_indexes 
        WHERE (owner like 'ZIPPY%')
        AND INDEX_TYPE = 'NORMAL' 
        ORDER BY owner, index_name;
    SQL_STMT VARCHAR2(200);
BEGIN
    DBMS_OUTPUT.ENABLE (100000);
    FOR IndexRec IN Index_Cur LOOP
        SQL_STMT := ('ALTER INDEX ' || IndexRec.owner || '.' || IndexRec.index_name || ' REBUILD');
        DBMS_OUTPUT.PUT_LINE(SQL_STMT);
        EXECUTE IMMEDIATE SQL_STMT;
        DBMS_OUTPUT.NEW_LINE;
    END LOOP;
 /

execute CJB_IDX_REBUILD;

Procedure created.

Elapsed: 00:00:00.01
zippy> execute CJB_IDX_REBUILD;
ALTER INDEX ZIPPY.A108_PK REBUILD
BEGIN CJB_IDX_REBUILD; END;

*
ERROR at line 1:
ORA-01418: specified index does not exist
ORA-06512: at "CJB.CJB_IDX_REBUILD", line 15
ORA-06512: at line 1

3 个答案:

答案 0 :(得分:1)

您以用户身份登录,只能通过角色更改这些索引;但是存储过程无法使用您的角色。该程序的所有者可能需要直接ALTER ANY INDEX批准。

答案 1 :(得分:0)

PL / SQL中匿名块和过程之间最常见的区别是: 匿名块生命仅用于当前会话,但过程将作为对象保存在数据库中。

答案 2 :(得分:0)

执行过程的用户无权更改。