Accessing and Changing Relational Data

Using IF...ELSE

The IF statement is used to test for a condition. The resulting flow of control depends on whether the optional ELSE statement is specified:

For example, if a stored procedure has been saving any error codes returned by @@ERROR during a transaction, it might have an IF statement similar to the following at the end of the procedure:

IF (@ErrorSaveVariable <> 0)
BEGIN
   PRINT 'Errors encountered, rolling back.'
   PRINT 'Last error encountered: ' +
      CAST(@ErrorSaveVariable AS VARCHAR(10))
   ROLLBACK
END
ELSE
BEGIN
   PRINT 'No Errors encountered, committing.'
   COMMIT
END
RETURN @ErrorSaveVariable

See Also

ELSE (IF...ELSE)

IF...ELSE