Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Stored Proc ERRORS

Author  Topic 

jesus4u
Posting Yak Master

204 Posts

Posted - 2002-08-27 : 16:14:12
In this SP transaction, if there is more than 1 error, will the @@Error contain all the errors or just the most recent error?

Thanks


CREATE PROCEDURE dbo.db_InsertPrivateMessage
@msg varchar(250),
@FromUid int,
@ToUid int,
@ViewId int,
@ChatRoomID int
AS

BEGIN TRANSACTION

DECLARE @sendMessages int, @CT int

SELECT @CT = Count(*)
FROM dbo.Questions INNER JOIN
dbo.Chatters ON dbo.Questions.Uid = dbo.Chatters.id
WHERE msg = @msg AND Uid = @FromUid AND ViewID = @ViewID

-- If no records already exists
IF @CT = 0
BEGIN

INSERT INTO
Questions
(msg,
Uid,
ReceiverID,
isPrivate,
ViewId)
VALUES
(@msg,
@FromUid,
@ToUid,
'True',
@ViewID)


-- update user's message count
SELECT @sendMessages = sendMessages FROM Chatters WHERE id = @FromUid AND Chatid = @ChatRoomID
-- increment count
SET @sendMessages = @sendMessages + 1
-- update user table
UPDATE Chatters SET lastAction = getdate(), sendMessages = @sendMessages WHERE id = @FromUid AND Chatid = @ChatRoomID

IF @@Error > 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END
END

COMMIT TRANSACTION
GO



Onamuji
Aged Yak Warrior

504 Posts

Posted - 2002-08-27 : 16:19:45
Just the most recent error ... here's how I handle transactions and error catching ...

DO DATA MODIFICATION STATEMENT (INSERT|UPDATE|DELETE)
PUT @@ERROR INTO LOCAL VARIABLE
IF LOCAL VARIABLE IS NON-ZERO VALUE THEN ROLLBACK AND RETURN THE ERROR
ELSE CONTINUE ON
REPEAT AS NEEDED

I do this for every INSERT|UPDATE|DELETE statement in a batch transaction ... then it will catch any error ... you have to remember to put the @@ERROR variable into a local variable if you intend on using it ... because the VERY next command after @@ERROR is set will re-set it to whatever happens...

Say you do ...

UPDATE SomeTable SET Column1 = 1 WHERE Column2 = 5

IF @@ERROR <> 0 -- This causes @@ERROR to be reset to 0 after the test is done ... if @@ERROR != 0 then you will go into the IF ... however @@ERROR will be zero as soon as you get in the IF so you won't know what error brought you into the IF...


Go to Top of Page
   

- Advertisement -