0

I have one batch file with contents like:

@Echo Off    
goto skipcomments
rem  
:skipcomments

call setup-CIpatch-db.bat
if not "%UserName%"=="" goto ok

echo You must edit setup-CIpatch-db.bat to set variables.  
echo This script no longer takes command line arguments.
goto end
:ok


if exist *.out  del *.out

echo CoreIssue FleetOne Maintenance Release
echo working... DO NOT CLOSE
REM ************************************************
REM Script in which Print Statement has been removed
REM ************************************************


REM ************************************************
REM Create Scripts for Tables
REM ************************************************


REM ************************************************
REM Alter Scripts for Tables
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i AlterTablecardBacktable07312012.sql -o AlterTablecardBacktable07312012.out





REM ************************************************
REM Data Updates
REM ************************************************

osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i AlterDrpandCrePrimKeyCardback.sql -o AlterDrpandCrePrimKeyCardback.out

REM ************************************************
REM Performance Updates
REM ************************************************




REM ************************************************
REM Security Update Scripts
REM ************************************************


REM ************************************************
REM Reports scripts
REM ************************************************



REM ************************************************
REM New or Altered Stored Procedures
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_RejectAccounts_qsel.sql -o sp_RejectAccounts_qsel.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_RejectAccounts_sel.sql -o sp_RejectAccounts_sel.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i sp_GetCompanyRecordCardRequest_01Aug2012.sql -o sp_GetCompanyRecordCardRequest_01Aug2012.out
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i SPGetCreditTransactions.sql -o SPGetCreditTransactions.out




REM ************************************************
REM Grant.sql for new or altered SP
REM ************************************************


REM ************************************************
REM Platform TranID Change Scripts
REM ************************************************


REM ************************************************
REM Version number update
REM ************************************************
osql -S %ServerName% -d %DBName% -U %sql_login% -P %sql_passwd% -n -i version.sql -o version.out


echo All Scripts have been started 
:end
pause

The script executes SQL files in the same folder and generates files with the extension of '.out'. These just contain the result; the same as when we execute any query in SSMS messages window, like 1 row affected etc. After the batch file has finished executing all scripts (via ADO.net/C#), we have to check the out files for keywords like "error" etc.

Now I have been given task to automate this and here is what I am doing:
Fetch the .SQL file name in tha batch file and execute them using SqlConnection, SqlCommand etc. The problem is that when the query has any error in it, an exception is thrown. What I need is that the query should execute whatsoever and I should be able to get the results. (like 1 row affected etc). Like in SSMS, when we execute any bad query, it executes and show the error in the Messages Pane.

Can anyone guide me ?

4

1 回答 1

0

查看MSDN上的文档,oSql支持 2 个参数-m-r这应该可以让您更好地处理错误输出。

此外,作为批处理脚本的一部分,您可以检查错误级别,如下所示

注意:我使用标签创建了一个函数来执行我的脚本,而无需重复所有错误代码

REM run list of scripts
call :UpgradeDatabase AlterTablecardBacktable07312012.sql
call :UpgradeDatabase version.sql

goto :eof

:UpgradeDatabase

set upgradeScript=%~1

echo Running script %upgradeScript% on database %databaseName%...
sqlcmd -S %serverName% -d %databaseName% -U %userName% -P %password% -i %upgradeScript% -o %logPath%\%upgradeScript%.log
if %errorlevel% == 0 (
    echo Script %upgradeScript% on database %databaseName% successful
) ELSE (
    echo Script %upgradeScript% on database %databaseName% FAILED
    echo. > %logPath%\ERROR_%upgradeScript%.log
)
echo.
echo.

goto :eof
于 2012-08-24T06:27:20.120 回答