需要在Oracle存储过程中放置​​TRY - CATCH

时间:2015-12-09 12:19:03

标签: sql oracle stored-procedures plsql

我创建了SP,用于在多个表上创建unique index

现在我需要为此创建Try-catch(异常处理),就像没有创建的incase索引一样,这应该是catch ....

例如,我有200个表,只有1个表给出错误,然后应该创建199个表,索引和catch日志有一个错误的表名。

请帮忙。

DECLARE
CURSOR C_TABLE IS
SELECT INPUT_TABLE,HISTORY_TABLE FROM FUNCTIONS WHERE TARGET_SYS IN ('ABC','DEC') AND ACTIVE_FLAG='Y';

SQL_CREATE_INX VARCHAR2(200);
SQL_TABLE_NAME VARCHAR2(200);

BEGIN
-- INPUT TABLE
    FOR I IN C_TABLE
    LOOP

        SQL_CREATE_INX:='CREATE UNIQUE INDEX CLM1.AUDIT_SUB_SITE_INX ON '||I.INPUT_TABLE||' (AUDIT_NBR , SUB_AUDIT_NBR , STATE) ';
        SQL_TABLE_NAME:=I.INPUT_TABLE;
        EXECUTE IMMEDIATE SQL_CREATE_INX;

        DBMS_OUTPUT.PUT_LINE('INDEX DONE : '||SQL_TABLE_NAME);

    END LOOP;
    DBMS_OUTPUT.PUT_LINE('INDEXES CREATED FOR ALL INPUT TABLES');

    -- OUTPUT TABLE
    FOR H IN C_TABLE
    LOOP

        SQL_CREATE_INX:='CREATE UNIQUE INDEX CLM1.AUDIT_SUB_SITE_INX ON '||H.HISTORY_TABLE||' (AUDIT_NBR , SUB_AUDIT_NBR , STATE)  ';
        SQL_TABLE_NAME:=H.HISTORY_TABLE;
        EXECUTE IMMEDIATE SQL_CREATE_INX;

        DBMS_OUTPUT.PUT_LINE('INDEX DONE : '||SQL_TABLE_NAME);

    END LOOP;
    DBMS_OUTPUT.PUT_LINE('INDEXES CREATED FOR ALL OUTPUT TABLES');


END;

2 个答案:

答案 0 :(得分:1)

您需要将所有EXEC IMMEDIATE括起来BEGIN .. END

  FOR (.... )
  LOOP
      /*
       *  Other Statements
       */
      BEGIN  /* try */
            EXECUTE IMMEDIATE SQL_CREATE_INX;

            /* If it is success the below output happens */
            DBMS_OUTPUT.PUT_LINE('INDEX DONE : '||SQL_TABLE_NAME);
      EXCEPTION /*catch */
      /* It is like catch(Exception e) All execeptions go here..*/
      WHEN OTHERS THEN
          /* Log your error message here.. SQLERRM has it..*/
          DBMS_OUTPUT.PUT_LINE('DDL FAILED FOR '||SQL_TABLE_NAME||'::FAILED WITH ERROR::'||SQLERRM);
      END;
       /*
        *  Other Statements
        */
   END LOOP;

答案 1 :(得分:0)

在oracle处理异常时如下所示

Declare
--declaration 
BEGIN
   --executable_section

EXCEPTION
   WHEN exception_name1 THEN
      [statements]

   WHEN exception_name2 THEN
      [statements]

   WHEN exception_name_n THEN
      [statements]

   WHEN OTHERS THEN
      [statements]

END 

通过编辑您的程序,异常块位于

之下
    DECLARE
    CURSOR C_TABLE IS
    SELECT INPUT_TABLE,HISTORY_TABLE FROM FUNCTIONS WHERE TARGET_SYS IN ('ABC','DEC') AND ACTIVE_FLAG='Y';
    var1 number;
    var2 number;
    SQL_CREATE_INX VARCHAR2(200);
    SQL_TABLE_NAME VARCHAR2(200);

    BEGIN
    -- INPUT TABLE
        FOR I IN C_TABLE
        LOOP
    select 1 into var1 from user_tables where table_name=upper(i.INPUT_TABLE)
  if var1=1
 then
            SQL_CREATE_INX:='CREATE UNIQUE INDEX CLM1.AUDIT_SUB_SITE_INX ON '||I.INPUT_TABLE||' (AUDIT_NBR , SUB_AUDIT_NBR , STATE) ';
            SQL_TABLE_NAME:=I.INPUT_TABLE;
            EXECUTE IMMEDIATE SQL_CREATE_INX;

            DBMS_OUTPUT.PUT_LINE('INDEX DONE : '||SQL_TABLE_NAME);
    end if;
        END LOOP;
        DBMS_OUTPUT.PUT_LINE('INDEXES CREATED FOR ALL INPUT TABLES');

        -- OUTPUT TABLE
        FOR H IN C_TABLE
        LOOP
     select 1 into var2 from user_tables where table_name=upper(h.HISTORY_TABLE)
  if var2=1
 then
            SQL_CREATE_INX:='CREATE UNIQUE INDEX CLM1.AUDIT_SUB_SITE_INX ON '||H.HISTORY_TABLE||' (AUDIT_NBR , SUB_AUDIT_NBR , STATE)  ';
            SQL_TABLE_NAME:=H.HISTORY_TABLE;
            EXECUTE IMMEDIATE SQL_CREATE_INX;

            DBMS_OUTPUT.PUT_LINE('INDEX DONE : '||SQL_TABLE_NAME);
    end if;
        END LOOP;
        DBMS_OUTPUT.PUT_LINE('INDEXES CREATED FOR ALL OUTPUT TABLES');
    exception 
   WHEN OTHERS THEN
    Dbms_output.put_line(sqlerrm ||' error occured with this error code '||SQLCODE);

END;

一些Predefined exceptions in oracle并使用您的例外名称编辑plsql块。