1. @@ERROR returns the error number for the last SQL statement executed.

2. Return type of @@ERROR is int.

3. If the statement executed successfully @@ERROR is set to 0.

4. If an error occurs, an error is returned.

5. You can view the details associated with the error in the table called sysmessages.

6. USE MASTER
SELECT * FROM sysmessages where error = –Error number you get.

7. You can also use SELECT * FROM master.dbo.sysmessages

8. If you want to use error message later , save it in a local variable.

Below example shows how you can use the @@Error to handle the error handling in your application at database level.

________________________________

select * from master.dbo.sysmessages

DECLARE @InputValue AS INT , @Output AS INT , @Interm AS INT
SET @InputValue = 20
SET @Output = @InputValue / 0
SET @Interm = @@ERROR
PRINT @Interm
IF @Interm <> 0
BEGIN
PRINT ‘Output’ + SPACE(1) + ‘:’ + SPACE(1) + CAST(@Interm AS VARCHAR(20))
END
ELSE
BEGIN
PRINT ‘Output’ + SPACE(1) + ‘:’ + SPACE(1) + CAST(@Output AS VARCHAR(20))
END

________________________________

We all are here to learn.

Pawan Kumar
Pawankkmr@hotmail.com