Solved

Rollback and insert error message into table

Posted on 2007-11-14
4
208 Views
Last Modified: 2008-02-01
I have a stored procedure that inserts does some inserts, updates and deletes.
If there is an error, I would like to record the error to the messagelog table
and rollback the transaction.

How can I do this?


BEGIN TRANSACTION

--Insert some data

--Insert some more data

--Delete some data

--update some data


If @@Error = 0
  COMMIT TRANSACTION
ELSE
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
0
Comment
Question by:JRockFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 150 total points
ID: 20280404
to start with, you will need to use the BEGIN END as you have more than 1 statement.

If @@Error = 0
  COMMIT TRANSACTION
ELSE
BEGIN
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
END


now, the error message itself will be tricky...
but, check out this cool function:
http://www.nigelrivett.net/SQLTsql/spFormatOutputBuffer.html
0
 
LVL 75

Assisted Solution

by:Aneesh Retnakaran
Aneesh Retnakaran earned 150 total points
ID: 20280549
BEGIN TRANSACTION

--Insert some data
IF  @@ERROR<>0 -- OR @@ROWCOUNT = 0
BEGIN
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
 
  RETURN  
END

--Insert some more data
BEGIN
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
 
  RETURN  
END
--Delete some data

BEGIN
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
 
  RETURN  
END
--update some data

BEGIN
  ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC)
  (@MESSAGE, @SPROC)
 
  RETURN  
END

  COMMIT TRANSACTION
0
 
LVL 6

Accepted Solution

by:
Rajesh_mj earned 200 total points
ID: 20287989
Hi,

Declare @AnyError Int,@MESSAGE varchar(100), @SPROC varchar(100)
SET @AnyError = 0

BEGIN TRANSACTION

--Insert some data
If @@ERROR <> 0
GOTO Err

--Insert some more data
If @@ERROR <> 0
GOTO Err

--Delete some data
If @@ERROR <> 0
GOTO Err

--update some data
If @@ERROR <> 0
GOTO Err

  COMMIT TRANSACTION
  RETURN       
      
Err:
 ROLLBACK TRANSACTION
  --would like to insert error into message log
  INSERT INTO MessageLog
  (Message, SPROC) values
  (@MESSAGE, @SPROC)
0
 
LVL 6

Expert Comment

by:Rajesh_mj
ID: 20287995
Sorry,
@AnyError variable is not required in my last comment
0

Featured Post

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question