SQLPlus Error Handle

Source: Internet
Author: User
Tags sql error sqlplus

SQLPlus directive "whenever SQLERROR EXIT 1" would 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 corres Pondence code.

Below is a example, Gpdemo.sql is a normal SQL file, it'll be execute by Sqlplus called from Main.bat.

The Main.bat would 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 Pro tected] @gpdemo. SQL >> DROPUSER.log:: RELOAD dump file to gpdemoif ERRORLEVEL 1 (    ECHO "Failed to Drop user, Che CK the DROPUSER.log for detail error message ") Else (    ECHO Drop User successfully, would continute to do import"    I MP System/[email protected] fromuser=gpcomp1 touser=gpdemo file=gpdemo.dmp log=gpdemo.log statistics=none)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.