INF: Global Variable @@error Checks for Successful Completion

ID: Q64842


The information in this article applies to:
  • Microsoft Windows NT versions 3.1, 3.5, 3.51, 3.51 SP2, 3.51 SP5, 4.0, 4.0 Service Pack 6, 4.0 Service Pack 6a, 4.0 SP4, 5.0, SP3
  • Microsoft SQL Server version 4.2x


SUMMARY

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.


MORE INFORMATION

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>)

The resulting set contains all rows returned from the "titles" table and all rows returned from the "roysched" table. Both tables are located in the sample pubs database.

The second example, ERR2.CMD, will attempt to execute the first SELECT * command on the table provided by the first table_name parameter, but the global variable @@error will have been set to 1, indicating that an error has been encountered. Processing of the control-of-flow language will stop, and no records will be retrieved for either table. The error is encountered because there is a syntax error in the word SELECT. Instead of using the proper spelling of SELECT, it is misspelled as "seleect". To execute this batch file, type the following at the command prompt:
err2 titles roysched

As indicated above, the resulting set will be empty because an error was encountered.

Example 1: ERR1.CMD

The global variable @@error will have the value of "0" (zero), indicating that the first SELECT command completed successfully; thus, all records will be retrieved from both tables.

@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 

Example 2: ERR2.CMD

The global variable @@error will have the value of "1", indicating that an error was encountered in the first SELECT command (there is a syntax error in the word SELECT in the first SELECT command); therefore, the second SELECT will not be executed.

@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 kbNTOS310 kbNTOS350 kbNTOS351 kbNTOS400 kbNTOS400sp1 kbNTOS400sp2 kbNTOS500sp3 kbNTOS400sp4 SSrvWinNT kbNTOS400sp6 kbNTOS400sp5 kbNTOS400sp6a
Version : winnt:3.1,3.5,3.51,3.51 SP2,3.51 SP5,4.0,4.0 SP4,4.0 Service Pack 6,4.0 Service Pack 6a,4.2x,5.0,SP3
Platform : winnt
Issue type :


Last Reviewed: January 19, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.