捕获错误并继续使用 sqlcmd :r
Capture error and continue with sqlcmd :r
我有以下批处理文件和运行多个 .sql 文件的 SQL 脚本。有没有办法记录任何错误并继续脚本?
myautosql.sql
PRINT 'Starting...'
--:On Error exit
:r SQLQuery10.sql
:r SQLQuery20.sql
:r SQLQuery30.sql
PRINT 'Completed normally on ' + (CONVERT( VARCHAR(24), GETDATE(), 101))
GO
myautosql.bat
SQLCMD -E -d rstestdb1 -i myautosql.sql
PAUSE
当我在 SQLQuery20.sql 文件中故意 raiseerror
时,批处理程序停止。有没有办法让它记录错误并继续脚本?
当你raiserror
时,第二个参数severity
决定查询是否会继续到运行。 0-10 的 severity
是信息性的(不引发错误),11-19 是非致命错误,20-25 将引发错误,然后立即终止与数据库服务器的连接。您必须是 sysadmin
才能使用 19-25 岁的 severity
。
我认为这模拟了您正在尝试做的事情。
auto.sql
PRINT 'Starting...'
:r 1.sql
:r 2.sql
PRINT 'Completed normally on ' + (CONVERT( VARCHAR(24), GETDATE(), 101))
1.sql
select 1 as value
raiserror ('This will not stop execution', 11, 1)
select 2 as value
2.sql
select 3 as value
raiserror ('This is an information message, not an error', 10, 1)
select 4 as value
然后你 运行 下面的命令将查询输出捕获到 output.txt
和 informational/error 消息到 error.txt
:
sqlcmd -E -d tempdb -i auto.sql -r1 >output.txt 2>error.txt
-r1
告诉 sqlcmd
将 informational/error 消息重定向到 STDERR
.
>output.txt
将 STDOUT 从查询(包括受影响的行数)重定向到名为 output.txt
.
的文件
2>error.txt
将 STDERR 重定向到名为 error.txt
.
的文件
这是上述脚本中的两个文件:
output.txt
value
-----------
1
(1 rows affected)
value
-----------
2
(1 rows affected)
value
-----------
3
(1 rows affected)
value
-----------
4
(1 rows affected)
error.txt
Starting...
Msg 50000, Level 11, State 1, Server NAME, Line 4
This will not stop execution
This is an information message, not an error
Completed normally on 02/27/2020
HTH
我有以下批处理文件和运行多个 .sql 文件的 SQL 脚本。有没有办法记录任何错误并继续脚本?
myautosql.sql
PRINT 'Starting...'
--:On Error exit
:r SQLQuery10.sql
:r SQLQuery20.sql
:r SQLQuery30.sql
PRINT 'Completed normally on ' + (CONVERT( VARCHAR(24), GETDATE(), 101))
GO
myautosql.bat
SQLCMD -E -d rstestdb1 -i myautosql.sql
PAUSE
当我在 SQLQuery20.sql 文件中故意 raiseerror
时,批处理程序停止。有没有办法让它记录错误并继续脚本?
当你raiserror
时,第二个参数severity
决定查询是否会继续到运行。 0-10 的 severity
是信息性的(不引发错误),11-19 是非致命错误,20-25 将引发错误,然后立即终止与数据库服务器的连接。您必须是 sysadmin
才能使用 19-25 岁的 severity
。
我认为这模拟了您正在尝试做的事情。
auto.sql
PRINT 'Starting...'
:r 1.sql
:r 2.sql
PRINT 'Completed normally on ' + (CONVERT( VARCHAR(24), GETDATE(), 101))
1.sql
select 1 as value
raiserror ('This will not stop execution', 11, 1)
select 2 as value
2.sql
select 3 as value
raiserror ('This is an information message, not an error', 10, 1)
select 4 as value
然后你 运行 下面的命令将查询输出捕获到 output.txt
和 informational/error 消息到 error.txt
:
sqlcmd -E -d tempdb -i auto.sql -r1 >output.txt 2>error.txt
-r1
告诉 sqlcmd
将 informational/error 消息重定向到 STDERR
.
>output.txt
将 STDOUT 从查询(包括受影响的行数)重定向到名为 output.txt
.
2>error.txt
将 STDERR 重定向到名为 error.txt
.
这是上述脚本中的两个文件:
output.txt
value
-----------
1
(1 rows affected)
value
-----------
2
(1 rows affected)
value
-----------
3
(1 rows affected)
value
-----------
4
(1 rows affected)
error.txt
Starting...
Msg 50000, Level 11, State 1, Server NAME, Line 4
This will not stop execution
This is an information message, not an error
Completed normally on 02/27/2020
HTH