Returns 0 if the previous Transact-SQL statement encountered no errors. --------------------------------------------------------------------------------- A. Use @@ERROR to detect a specific error USE AdventureWorks; GO UPDATE HumanResources.Employee SET PayFrequency = 4 WHERE NationalIDNumber = 615389812; IF @@ERROR = 547 PRINT N'A check constraint violation occurred.'; GO --------------------------------------------------------------------------------- B. Use @@ERROR to conditionally exit a procedure USE AdventureWorks; GO -- Drop the procedure if it already exists. IF OBJECT_ID(N'HumanResources.usp_DeleteCandidate', N'P') IS NOT NULL DROP PROCEDURE HumanResources.usp_DeleteCandidate; GO -- Create the procedure. CREATE PROCEDURE HumanResources.usp_DeleteCandidate @CandidateID INT AS -- Execute the DELETE statement. DELETE FROM HumanResources.JobCandidate WHERE JobCandidateID = @CandidateID; -- Test the error value. IF @@ERROR <> 0 BEGIN -- Return 99 to the calling program to indicate failure. PRINT N'An error occurred deleting the candidate information.'; RETURN 99; END ELSE BEGIN -- Return 0 to the calling program to indicate success. PRINT N'The job candidate has been deleted.'; RETURN 0; END; GO --------------------------------------------------------------------------------- C. Use @@ERROR with @@ROWCOUNT USE AdventureWorks; GO IF OBJECT_ID(N'Purchasing.usp_ChangePurchaseOrderHeader',N'P') IS NOT NULL DROP PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader; GO CREATE PROCEDURE Purchasing.usp_ChangePurchaseOrderHeader @PurchaseOrderID INT, @EmployeeID INT AS -- Declare variables used in error checking. DECLARE @ErrorVar INT, @RowCountVar INT; -- Execute the UPDATE statement. UPDATE PurchaseOrderHeader SET EmployeeID = @EmployeeID WHERE PurchaseOrderID = @PurchaseOrderID; -- Save the @@ERROR and @@ROWCOUNT values in local -- variables before they are cleared. SELECT @ErrorVar = @@ERROR, @RowCountVar = @@ROWCOUNT; -- Check for errors. If an invalid @EmployeeID was specified -- the UPDATE statement returns a foreign-key violation error #547. IF @ErrorVar <> 0 BEGIN IF @ErrorVar = 547 BEGIN PRINT N'ERROR: Invalid ID specified for new employee.'; RETURN 1; END ELSE BEGIN PRINT N'ERROR: error ' + RTRIM(CAST(@ErrorVar AS NVARCHAR(10))) + N' occurred.'; RETURN 2; END END -- Check the row count. @RowCountVar is set to 0 -- if an invalid @PurchaseOrderID was specified. IF @RowCountVar = 0 BEGIN PRINT 'Warning: The EmployeeID specified is not valid'; RETURN 1; END ELSE BEGIN PRINT 'Purchase order updated with the new employee'; RETURN 0; END; GO ---------------------------------------------------------------------------------