SQLPlus Error handle

來源:互聯網
上載者:User

標籤:style   blog   color   io   for   ar   cti   div   

SQLPlus directive "WHENEVER SQLERROR EXIT 1" will return a specified code when any SQL error throwed when run a sql file.

Then we can catch the return code from the main bat file (where we call the sqlplus) to test it and then jump to a  correspondence code.

Below is a example, gpdemo.sql is a normal sql file, it will be execute by sqlplus called from main.bat.

The main.bat will test the return code to know whether any sql error occurred when execute the gpdemo.sql.

gpdemo.sql

WHENEVER SQLERROR EXIT 1;DECLARE  V_USER_EXISTED INT;BEGIN    SELECT COUNT(1) INTO V_USER_EXISTED FROM DBA_USERS WHERE USERNAME=‘GPDEMO‘;        IF V_USER_EXISTED >= 1 THEN            EXECUTE IMMEDIATE ‘DROP USER GPDEMO CASCADE‘;    END IF;    EXECUTE IMMEDIATE ‘CREATE USER GPDEMO IDENTIFIED BY SUNGARD01‘;        EXECUTE IMMEDIATE ‘GRANT CREATE TABLE TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT CREATE VIEW TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT CREATE PROCEDURE TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT CREATE SEQUENCE TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT CREATE SYNONYM TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT CREATE SESSION TO GPDEMO‘;        EXECUTE IMMEDIATE ‘GRANT GETPAID_ROLE TO GPDEMO‘;        EXECUTE IMMEDIATE ‘ALTER USER GPDEMO DEFAULT TABLESPACE GP_DATA‘;        EXECUTE IMMEDIATE ‘ALTER USER GPDEMO TEMPORARY TABLESPACE TEMP‘;        EXECUTE IMMEDIATE ‘ALTER USER GPDEMO QUOTA UNLIMITED ON GP_DATA‘;        EXECUTE IMMEDIATE ‘ALTER USER GPDEMO QUOTA UNLIMITED ON GP_INDX‘;END;/EXIT 0

main.bat

:: Clear the ERRORLEVEL environment if it had defined.set ERRORLEVEL =:: Drop and recreate GPDEMOsqlplus system/[email protected] @gpdemo.sql >> DROPUSER.log:: RELOAD dump file to GPDEMOif ERRORLEVEL 1 (    ECHO "Failed to Drop user, check the DROPUSER.log for detail error message") else (    ECHO "Drop User Successfully, will continute to do import"    imp system/[email protected] fromuser=gpcomp1 touser=GPDEMO file=gpdemo.dmp log=gpdemo.log statistics=none)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.