INF: Global Variable @@error Checks for Successful CompletionID: Q64842
|
This article discusses how to check to see if a Transact-SQL command
completed successfully when executed within a command file.
The global variable @@error can be tested to check for the successful
completion of a Transact-SQL command within a command file using the
control-of-flow language.
The two examples included below illustrate the use of the global
variable @@error. The first example, ERR1.CMD, will execute the
Transact-SQL "select *" command on the table names supplied from the
command line. For example, to execute this command file, type the
following at the command prompt:
err1 titles roysched
(Format: <batch_file_name> <table_name> <table_name>)
err2 titles roysched
@echo off
set tb1=%1
set tb2=%2
echo /* Selecting records for tables: %tb1% and %tb2% */
echo use pubs >isql.in
echo go >>isql.in
echo begin >>isql.in
echo select * >>isql.in
echo from %tb1% >>isql.in
echo end >>isql.in
echo go >>isql.in
echo begin >>isql.in
echo If @@error !=0 >>isql.in
echo print "error" >>isql.in
echo else >>isql.in
echo if @@error = 0 >>isql.in
echo select * from %tb2% >>isql.in
echo end >>isql.in
echo go >>isql.in
isql /Usa /P /n /i isql.in
@echo off
set tb1=%1
set tb2=%2
echo /* Selecting records for tables: %tb1% and %tb2% */
echo use pubs >isql.in
echo go >>isql.in
echo begin >>isql.in
echo seleect * >>isql.in
echo from %tb1% >>isql.in
echo end >>isql.in
echo go >>isql.in
echo begin >>isql.in
echo If @@error !=0 >>isql.in
echo print "error" >>isql.in
echo else >>isql.in
echo if @@error = 0 >>isql.in
echo select * from %tb2% >>isql.in
echo end >>isql.in
echo go >>isql.in
isql /Usa /P /n /i isql.in
Additional query words: Transact-SQL Windows NT
Keywords : kbprg SSrvProg SSrvTran_SQL SSrvWinNT
Version : 4.2 | 4.2
Platform : OS/2 WINDOWS
Issue type :
Last Reviewed: March 9, 1999