?
Solved

Rollback and insert error message into table

Posted on 2007-11-14
4
Medium Priority
?
213 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
  • 2
4 Comments
 
LVL 143

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 600 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 600 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 800 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example, show how to setup several different housekeeping processes for a 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.
Suggested Courses

807 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