G
Guest
Hi
We have recently moved onto SQL 2005 and our DBA wants us to use Try Catch
within our stored procedures.
If I have an SP such as:
CREATE PROCEDURE NoTry
AS
BEGIN
INSERT INTO MyTable(Id, Description)
VALUES (3, 'Toodles');
END
and I have a primary key on the Id field and record 3 already exists. When
I call it from ADO.Net it will result in a SQLException being thrown with the
approrpiate Constraint Violation details shown.
Now if I wrap it in a Try Catch block like
CREATE PROCEDURE TryCatch
AS
BEGIN
BEGIN TRY
INSERT INTO MyTable(Id, Description)
VALUES (3, 'Toodles');
END TRY
BEGIN CATCH
-- Log the error / rollback transaction etc.
END CATCH
END
As you would expect the exception is now not raised to ADO.Net as it is
handled by the SP.
My question is if I need to re-raise the error how do I do it or
alternatively how do I signal to ADO.Net that an error has occurred.
My first thought was to use RAISEERROR in the CATCH block however this
results in a 50000 error not the original error. Based on this my current
approach is concatinating all the error information in the message string.
Then have code in my .Net Catch block checking if it is a 50000 error and
unpacking the exception.
An alternative was to simply set the retun value to the error number.
My final thought was to ignore TRY CATCH functionality in SQL totally
however that seems extreme and self-defeating.
Have I missed something and it is possible to Reraise a SQL error?
Has anybody got any best practices on the best approach to take?
Thanks in advance
Phil
We have recently moved onto SQL 2005 and our DBA wants us to use Try Catch
within our stored procedures.
If I have an SP such as:
CREATE PROCEDURE NoTry
AS
BEGIN
INSERT INTO MyTable(Id, Description)
VALUES (3, 'Toodles');
END
and I have a primary key on the Id field and record 3 already exists. When
I call it from ADO.Net it will result in a SQLException being thrown with the
approrpiate Constraint Violation details shown.
Now if I wrap it in a Try Catch block like
CREATE PROCEDURE TryCatch
AS
BEGIN
BEGIN TRY
INSERT INTO MyTable(Id, Description)
VALUES (3, 'Toodles');
END TRY
BEGIN CATCH
-- Log the error / rollback transaction etc.
END CATCH
END
As you would expect the exception is now not raised to ADO.Net as it is
handled by the SP.
My question is if I need to re-raise the error how do I do it or
alternatively how do I signal to ADO.Net that an error has occurred.
My first thought was to use RAISEERROR in the CATCH block however this
results in a 50000 error not the original error. Based on this my current
approach is concatinating all the error information in the message string.
Then have code in my .Net Catch block checking if it is a 50000 error and
unpacking the exception.
An alternative was to simply set the retun value to the error number.
My final thought was to ignore TRY CATCH functionality in SQL totally
however that seems extreme and self-defeating.
Have I missed something and it is possible to Reraise a SQL error?
Has anybody got any best practices on the best approach to take?
Thanks in advance
Phil