我想在一个批处理文件/脚本中逐个执行15个“bcp命令”。每个命令只有在上一个命令成功执行后才能处理或执行..如果在任何情况下命令都无法执行...脚本应该停止执行......
我试过这个..但我没有得到足够的输出我需要的..
bcp.exe "select * from OrderXpress.dbo.Customers where CustId < 1000" queryout "D:\Customer.dat" -S localhost -U sa -P Sa12345 -E -n
IF %ERRORLEVEL% > 0 ( PAUSE )
bcp.exe OrderXpress.dbo.Customers out "D:\Customer2.dat" -S localhost -U sa -P Sa12345 -E -n
IF %ERRORLEVEL% > 0 ( PAUSE )
bcp.exe OrderXpress.dbo.Orders out "D:\Orders.dat" -S localhost -U sa -P Sa12345 -E -n
IF %ERRORLEVEL% > 0 ( PAUSE )
我相信你缺少的是总脚本停止。您的pause
命令暂时保存脚本,但不保存完整脚本。
您应该使用标签来跳转。在下面的脚本中,我添加了标签:error_happened
以在出错时跳转到。如果没有错误,那么命令goto :EOF
将跳过:error_happened
标签。
bcp.exe "select * from OrderXpress.dbo.Customers where CustId < 1000" queryout "D:\Customer.dat" -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
bcp.exe OrderXpress.dbo.Customers out "D:\Customer2.dat" -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
bcp.exe OrderXpress.dbo.Orders out "D:\Orders.dat" -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
goto :EOF
:error_happened
echo,
echo An error has occurred. Script stopped
echo
pause
但是,必须对BCP出现的错误级别进行评论。如果没有正确导出数据行,那么我不确定errorlevel会发生什么。我知道在导入行错误不会导致问题。另见:https://groups.google.com/forum/#!topic/microsoft.public.sqlserver.tools/qzpWuZzJnr4
我有一个解决方法使用-e参数来检查错误。该脚本将是:
bcp.exe "select * from OrderXpress.dbo.Customers where CustId < 1000" queryout "D:\Customer.dat" -e errors.txt -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
call :check_errorfile
bcp.exe OrderXpress.dbo.Customers out "D:\Customer2.dat" -e errors.txt -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
call :check_errorfile
bcp.exe OrderXpress.dbo.Orders out "D:\Orders.dat" -e errors.txt -S localhost -U sa -P Sa12345 -E -n
IF ERRORLEVEL 1 GOTO error_happened
call :check_errorfile
goto :EOF
:check_errorfile
if exist errors.txt (
FOR %%A IN (errors.txt) DO (
if %%~zA GTR 0 (
goto error_happened
) else (
del errors.txt
)
)
)
exit /b
:error_happened
echo,
echo An error has occurred. Script stopped
echo
pause
请试试这个......
BEGIN尝试bcp.exe“select * from OrderXpress.dbo.Customers where CustId <1000”queryout“D:\ Customer.dat”-S localhost -U sa -P Sa12345 -E -n
结束
开始捕捉
DECLARE @msg VARCHAR(1000)
SET @msg = ERROR_MESSAGE()
RAISERROR(@msg,16,0)
结束捕捉