Another possible option is to use a table variable to capture the information you want to keep in your persistent log table. Table variables are not returned if the ROLLBACK TRANSACTION command is given. Sample code below ...
--- Declare table variable DECLARE @ErrorTable TABLE ( [DATE] smalldatetime, [ENTRY] varchar(64) ) DECLARE @nErrorVar int --- Open Transaction BEGIN TRANSACTION --- Pretend to cause an error and catch the error code SET @nErrorVar = 1 --- @@ERROR IF (@nErrorVar = 1) BEGIN --- Insert error info table variable INSERT INTO @ErrorTable ( [Date], [Entry] ) SELECT getdate(), 'Error Message Goes Here' RAISERROR('Error Message Goes Here', 16, 1) ROLLBACK TRANSACTION --- Change this to actually insert into your permanent log table SELECT * FROM @ErrorTable END IF @@TRANCOUNT 0 PRINT 'Open Transactions Exist' ELSE PRINT 'No Open Transactions'
John franklin
source share