Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving @@ERROR and @@ROWCOUNT After Operation

Status
Not open for further replies.

Hexonx

Programmer
Joined
Jan 10, 2001
Messages
102
Location
US
Here's a helpful tip that TomSARK gave to me. In my procedure, I update a table, then, if the table was actually updated, I insert an audit record. But if an error occurred, I want to trap it. My problem is in evaulating both @@ERROR and @@ROWCOUNT because using SET with one would reset the other.

Here's the original:

-- Update the table
SET @SQL = 'UPDATE MyTable ...'

EXEC( @SQL )

SET @Err = @@ERROR -- Gets error status from EXEC(), but resets
@@ROWCOUNT to 1
SET @RowCount = @@ROWCOUNT

IF @Err <> 0 GOTO ErrHandler

IF @RowCount > 0
-- Insert audit record

Here's what works:

-- Update the table
SET @SQL = 'UPDATE MyTable ...'

EXEC( @SQL )

SELECT @Err = @@ERROR, @RowCount = @@ROWCOUNT

IF @Err <> 0 GOTO ErrHandler

IF @RowCount > 0
-- Insert audit record

Many thanks to TomSARK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top