- Basic syntax
- Key See-B On Error batch abort and-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@re M 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.netr Em @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_N Ame% ran Failed@goto exit:good@echo file%sql_name% ran Successfully@goto Exit:exit@echo exit
Case 1
- Case2
About SQL Server SQLCMD exception snapping%errorlevel%