How to process with error in MySQL bat file?

Viewed 138

I have such a .bat file

@echo off
set sql_bin_path=%MY_SQL%
cd /d %sql_bin_path%

mysqldump -u root -p --no-data test > test.sql


pause
goto :eof

:error
echo Something went wrong
pause

I need to catch a possible error in this line mysqldump -u root -p --no-data test > test.sql (for example if there isn't such db name) and move the execution to goto :error block

How to do it?

1 Answers

The command mysqldump has a return status, which can be 0, 1 or 2.
That can be captured and based on that, you can decide what to do.

More explanation can be found in this ServerFault post.

In case if you use .bat file the solution is:

@echo off
set sql_bin_path=%MY_SQL%
cd /d %sql_bin_path%

mysqldump -u root -p --no-data test > test.sql

if %ERRORLEVEL% NEQ 0 (goto error)
echo MySQL was dumped successfully  

pause
goto :eof

:error
echo Something went wrong
pause  
Related