How to write Transactional Error Messages into a Error Log Table in Sql Server 2005?

A TRY Block - the TRY block contains the instructions that might cause an exception

A CATCH Block - if an exception occurs from one of the statements in the TRY block, control is branched to the CATCH block, where the exception can be handled, logged, and so on.


BEGIN TRY
BEGIN TRAN
INSERT Query COMMIT TRAN
END TRY

BEGIN CATCH
ROLLBACK TRAN
INSERT dbo.ErrorLog([ErrorNumber],[ErrorMessage],[ErrorSource],[ErrorLine])
VALUES (ERROR_NUMBER(), ERROR_MESSAGE(),ERROR_PROCEDURE(),ERROR_LINE())
END CATCH

1 comment:

hisubash said...

Thanks a lot my friend.
Its very usefull.