码迷,mamicode.com
首页 > 数据库 > 详细

关于 sql server sqlcmd 异常捕捉 %errorlevel%

时间:2015-03-04 11:09:36      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:sqlcmd errorlevel

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

技术分享

runsql.bat

@echo off
:start
cd %cd%
@echo current directory is %cd%
@echo start testing
@echo good example
rem 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 
:g1
rem if finished
echo run example1 successful, errorlevel=%errorlevel%
goto exit1
:b1
rem error message
@echo run example1 failed, errorlevel=%errorlevel%
goto exit1
rem exit1
:exit1
echo exit


  • 测试
  1. 技术分享
  • 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.net
    rem @set uname=batchap
    rem @set upwd=1qaz2WSX
    rem @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 
    :s9
    sqlcmd -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
    :s8
    sqlcmd -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
    :s7
    sqlcmd -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
    :s6
    sqlcmd -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
    :s5
    sqlcmd -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
    :s4
    sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 p4=%4  
    @goto end
    :s3
    sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  p3=%3 
    @goto end
    :s2
    sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 P2=%2  
    @goto end
    :s1
    sqlcmd -E -dmaster -W -b -m-1 -h-1 -s^| -i %SQL_NAME% -l60 -v P1=%1 
    @goto end
    :s0
    sqlcmd -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. 技术分享
  2. case2
  3. 技术分享

关于 sql server sqlcmd 异常捕捉 %errorlevel%

标签:sqlcmd errorlevel

原文地址:http://blog.csdn.net/wangzhpwang/article/details/44056531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!