关于 sql server 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
  • 测试
  • 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. case2
时间: 2024-07-30 10:17:59

关于 sql server sqlcmd 异常捕捉 %errorlevel%的相关文章

监控SQL Server数据库异常镜像状态发告警邮件

监控SQL Server数据库异常镜像状态发告警邮件 在部署了数据库镜像之后,我们需要监控参与镜像的主数据库和镜像数据库的状态,如果状态异常,发送告警邮件.那么这个脚本需要在主和镜像服务器上都运行. 目录视图sys.database_mirroring对SQL Server实例上的每个数据库都包含一行(包括系统数据库和未配置镜像的数据库),当然也包含所有镜像数据库的状态信息.我们可以查询该目录视图,对于每个异常状态的镜像数据库触发告警邮件.笔者的环境配置的是异步镜像,依赖于手动故障转移. 前提条

SQL Server连接异常

SQL Server 2005/2008 在C#代码中调用Open()方法打开数据库连接时(账户为sa),出现异常:异常信息如下: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并 且 SQL Server 已配置为允许远程连接. (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错) win7系统解决方案如下: 1.打开Sql server 管理配置器 或者在命令行输入:SQLS

SQL Server出现异常:The system cannot find the file specified

在查询一个DB中的Table时,SQL Server 抛出异常: The operating system returned error 2(The system cannot find the file specified.) to SQL Server during a read at offset 0x00000000204000 in file '\\ServerName\xxxx.ndf'. Additional messages in the SQL Server error log

SQL server 获取异常

一.try...... catch 获取异常信息 /*======================================== 相关错误消 息如下: ERROR_NUMBER() 返回错误号. ERROR_SEVERITY() 返回严重性. ERROR_STATE() 返回错误状态号. ERROR_PROCEDURE() 返回出现错误的存储过程或 触发器的名称. ERROR_LINE() 返回导致错误的例程中的行 号. ERROR_MESSAGE() 返回错误消息的完整文本. =====

SQL Server Profiler工具

一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问题: 调试T-SQL语句: 模拟重放SQL Server活动: 也可以使用SQL Profiler捕捉在SQL Server实例上执行的活动.这样的活动被称为Profiler跟踪. 1.Profiler跟踪 从开始=>所有程序=>Microsoft SQL Server 2008=>性能工具

SQL Server Profiler(转载)

SQL Server Profiler工具 一.SQL Profiler工具简介 SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问题: 调试T-SQL语句: 模拟重放SQL Server活动: 也可以使用SQL Profiler捕捉在SQL Server实例上执行的活动.这样的活动被称为Profiler跟踪. 1.Profiler跟踪 从开始=>所有程序=>Microsoft SQ

SQL Server Profiler工具【转】

一.SQL Profiler工具简介 转自:http://www.cnblogs.com/kissdodog/p/3398523.html SQL Profiler是一个图形界面和一组系统存储过程,其作用如下: 图形化监视SQL Server查询: 在后台收集查询信息: 分析性能: 诊断像死锁之类的问题: 调试T-SQL语句: 模拟重放SQL Server活动: 也可以使用SQL Profiler捕捉在SQL Server实例上执行的活动.这样的活动被称为Profiler跟踪. 1.Profil

SQL Server 2014 SP1 通过补丁KB3058865提供更新,SP1一文便知

Microsoft SQL Server 2014 SP1 更新: SQLServer2014SP1-KB3058865-architecture-language.exe 安装完成后版本 12.0.4100.1. 主要特性包如下: Microsoft® SQL Server® Backup to Windows® Azure® Tool Microsoft SQL Server Backup to Windows Azure Tool 支持备份到 Windows Azure Blob 存储,加

34. PowerShell -- SQL Server的使用(2)

参考: http://www.shangxueba.com/jingyan/105946.html 一.先不用SqlServerCmdletSnapin100这个SnapIn来写几个操作常用数据的脚本 1. 由于有读者问如何用PowerShell显示数据库中表,以下是一个简单函数供参考 #==============================================# SQL Server 2008 - PowerShell# 显示用户表#zivsoft#=============