当前位置: 代码迷 >> Ruby/Rails >> 关于 sql server sqlcmd 错误捕捉 %errorlevel%
  详细解决方案

关于 sql server sqlcmd 错误捕捉 %errorlevel%

热度:128   发布时间:2016-04-29 02:16:08.0
关于 sql server sqlcmd 异常捕捉 %errorlevel%

  • 基本语法
  • 关键看-b on error batch abort 和 -m errorlevel

runsql.bat

@echo off:startcd %cd%@echo current directory is %cd%@echo start testing@echo good examplerem good sql sqlcmd -E -dmaster -Q%1 -W -b -m-1 -h-1 -s^|rem if the query or batch file abort@if %errorlevel%==1  goto b1 @if %errorlevel%==0  goto g1 :g1rem if finishedecho run example1 successful, errorlevel=%errorlevel%goto exit1:b1rem error message@echo run example1 failed, errorlevel=%errorlevel%goto exit1rem exit1:exit1echo exit


  • 测试
  • straxsql.bat
  • @echo off@REM ******** ******** General Batch for Starting SQL ******** ******** @REM %1 is the name of SQL script file@rem SET SQL_NAME=%1@SET SQL_NAME=%1@SHIFT /1@REM SHIFT /1@REM P1... is parameters of SQLCMD,P1=%1...@rem c for count@set /a c=0@rem ******** ******** parameters for sql server login ******** ********  rem @set svrName=ryemsqapaxdb1.na.avonet.netrem @set uname=batchaprem @set upwd=1qaz2WSXrem @set db=apoms@set s= %1 %2 %3 %4 %5 %6 %7 %8 %9@FOR %%A IN (%s%) DO @set /a c=c+1@if %c%==9  goto s9@if %c%==8  goto s8@if %c%==7  goto s7@if %c%==6  goto s6@if %c%==5  goto s5@if %c%==4  goto s4@if %c%==3  goto s3@if %c%==2  goto s2@if %c%==1  goto s1@if %c%==0  goto s0 :s9sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7 p8=%8 p9=%9 @goto end:s8sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7 p8=%8  @goto end:s7sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6 p=%7  @goto end:s6sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5 p6=%6  @goto end:s5sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4 p5=%5  @goto end:s4sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4  @goto end:s3sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 @goto end:s2sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  @goto end:s1sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 @goto end:s0sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60@goto end:end @if %errorlevel%==1  goto bad@if %errorlevel%==0  goto good@goto exit:bad@echo file %SQL_NAME% ran failed@goto exit:good@echo file %SQL_NAME% ran successfully@goto exit:exit@echo exit 

    case 1
  1. case2
  2.  http://download.csdn.net/detail/wangzhpwang/8471369

  相关解决方案