Error Handling in Stored Procedures

greenspun.com : LUSENET : SQL Server Database Administration : One Thread

I have a simple question about error handling in SQL 7.0 and 6.5. I would like to handle my errors and pass back some return values to my front end. I have tried using the @@error variable to trap errors and return my own error code. However when a error occurs in an insert for example, the error is thrown straight back to the front end(whatever it is) and the if @@error .... statement is not executed.

In the case below I purposely try to insert all null parameters to a table which demands that some columns not be null.

Is it possible to trap these errors? or am I wasting my time trying to write code to catch them?

Insert Into Person (FirstName, MiddleInitial, LastName, fkGenderType, DateOfBirth, UserName) Values( @varFirstName, @chrMiddleInitial, @varLastName, @intGenderType, @dtmDateOfBirth, @varUserName) IF @@error <> 0 BEGIN return -900 END

Thank you, Paul Tribe

-- Anonymous, March 07, 2000

Answers

Paul,

I don't have the time to double check them at the moment, but the SQL Server Books Online articles, titled "@@ERROR (T-SQL)" and "Using @@ERROR" give examples of using @@Error.

Hope this helps,

Eric

-- Anonymous, March 07, 2000


Moderation questions? read the FAQ