问题
i hv a prob here which already took me days to solve it..
my explanation will be quite lengthy but i try to keep it short.
in my Oracle SQLdeveloper, i have a package name UT_BETWNSTR which contain:
create or replace
PACKAGE "UT_BETWNSTR"
IS
PROCEDURE ut_setup;
PROCEDURE ut_teardown;
PROCEDURE ut_betwnstr;
END UT_BETWNSTR;
and the package body is like this:
create or replace
PACKAGE BODY "UT_BETWNSTR"
IS
PROCEDURE ut_setup IS
BEGIN
NULL;
END;
PROCEDURE ut_teardown
IS
BEGIN
NULL;
END;
PROCEDURE ut_BETWNSTR IS
BEGIN
utAssert.eq (
'Typical valid usage',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 3,
END_IN => 5)
,
'abc'
);
utAssert.isnull (
'NULL start',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => NULL,
END_IN => 5)
);
utAssert.isnull (
'NULL end',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 2,
END_IN => NULL)
);
utAssert.isnull (
'End smaller than start',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 5,
END_IN => 2)
);
utAssert.eq (
'End larger than string length',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 3,
END_IN => 200)
,
'cdefg'
);
END ut_betwnstr;
END UT_BETWNSTR;
and the function name BETWNSTR is like this:
create or replace
FUNCTION BETWNSTR (
string_in IN VARCHAR2,
start_in IN INTEGER,
end_in IN INTEGER
)
RETURN VARCHAR2
IS
l_start PLS_INTEGER := start_in;
BEGIN
IF l_start = 0
THEN
l_start := 1;
END IF;
RETURN (SUBSTR (string_in, l_start, end_in - l_start + 1));
END;
in my C drive, i put a file name BETWNSTR.sql which contain:
connect hr/hr
SET SERVEROUTPUT ON
EXEC UTPLSQL.TEST('BETWNSTR',Recompile_in=>FALSE);
exit
and this is my batch file (also in C drive), name try.bat which contain:
@sqlplus /nolog @C:\betwnstr.sql
echo %errorlevel%
if errorlevel 0 goto Success
echo You Got Error
:Success
echo Good Job!!
pause
ok here comes the error
when i run try.bat, it will return the big FAILURE result as i purposely put
PROCEDURE ut_BETWNSTR IS
BEGIN
utAssert.eq (
'Typical valid usage',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 3,
END_IN => 5)
,
'abc'
);
instead of
PROCEDURE ut_BETWNSTR IS
BEGIN
utAssert.eq (
'Typical valid usage',
BETWNSTR(
STRING_IN => 'abcdefg',
START_IN => 3,
END_IN => 5)
,
'cde'
);
i did that in purpose so that i hope the cmd will echo out:
You Got Error
because got error in my code..but the errorlevel is echoing 0 which mean it is a success..
i know, my code now is depending on errorlevel..when errorlevel is 0 it will echo out Good Job..
what i want right now is, when i run the batch file, when it encounter an error, i can echo out You Got Error message..
how can i echo out "You Got Error" message when the errorlevel is always showing 0..
in short, i want to echo out error message without depending on errorlevel..
i am hoping anybody hv the solution for my problem could pls answer my question..
Thanks In Advance!
回答1:
I am not sure if this will work, but perhaps it is worth a shot. ( I do not have a windows machine, so I cannot test, but it looks about right ) This will depend on sql's ability to write to standard output. I don't know Oracle, so I don't know if you can get it to do this, but if you can, the little hack below should work, if you know the standard pattern of error/warning messages. If /nolog stops printing to standard output, take it out of the below snippet. Worth a shot. Good luck! :)
setlocal enabledelayedexpansion
some_text_signifying_error=whatever you can get sql to display to Standard Output
set msg=''
For 'eol=; tokens=1 delims=' %%e in ('@sqlplus /nolog @C:\betwnstr.sql ^| findstr /i /c:"!some_text_signifying_error!"') do (
set msg=!msg! %%e
)
if NOT !msg!='' echo !msg!
回答2:
All previous answers answered: "How do I handle errors in a batch file that calls sqlplus, when sqlplus doesn't set ERRRORLEVEL on error or failure?"
But you can actually make sqlplus exit on errors with a non-zero exit code using following sqlplus command:
whenever sqlerror exit sql.sqlcode
See also http://voidtech.wordpress.com/2013/03/06/execute-an-script-with-sqlplus-and-get-the-exitcode-to-the-environment/ I use it in this way on a windows machine with Oracle 10g:
sqlplus some_user/pw@tns_name @ some_script.sql
IF %ERRORLEVEL% NEQ 0 call some_action_on_error.vbs
来源:https://stackoverflow.com/questions/8350640/how-do-i-handle-errors-in-a-batch-file-that-calls-sqlplus-because-sqlplus-doesn