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)
 Error handling in nested stored procedures

Author  Topic 

podgehb
Starting Member

18 Posts

Posted - 2006-02-14 : 12:11:46
I have a problem with my error handling in nested stored procedures (SQL Server 2000).

Below is an example of stored procedures that are causing this problem. If you install the scripts and run sp1, you'll get 2 errors:
(1) Divide by zero error encountered. -- This is expected due to : select 1/0
(2) Transaction count after EXECUTE indicates that a COMMIT or ROLLBACK TRANSACTION statement is missing. Previous count = 1, current count = 0.

This 2nd error is the problem and I'm not sure what is it.

If I change "select 1/0" to "insert into TestTable(Col1) values ('hello')", where Col1 is an int, I get : Syntax error converting the varchar value 'hello' to a column of data type int. BUT I don't get that 2nd error about the COMMIT or ROLLBACK TRANSACTION statement missing. For this different error, my error handling works perfectly.

Does anyone know what is causing this problem, and how I should change my error handling to avoid it.

Thanks,
Craig



CREATE proc sp2
as

declare @err int
declare @retvalue int

if (@@trancount=0) begin transaction
select 1/0
select @err=@@error if (@err<>0) goto error
if (@@trancount>0) commit transaction

return 0 -- success
error:
if (@@trancount>0) rollback transaction
if (@err<>0) return @err -- error
else if (@retvalue<>0) return @retvalue -- error returned from SP

GO

CREATE proc sp1
as

declare @err int
declare @retvalue int

if (@@trancount=0) begin transaction
exec @retvalue = sp2
select @err=@@error if (@err<>0)or(@retvalue<>0) goto error
if (@@trancount>0) commit transaction

return 0 -- success
error:
if (@@trancount<>0) rollback transaction
if (@err<>0) return @err -- error
else if (@retvalue<>0) return @retvalue -- error returned from SP

mallier
Starting Member

24 Posts

Posted - 2006-02-15 : 07:20:29
check this below link for errors which sql server 2000 @@error can handle.@@error cannot handle conversion errors.

http://www.sommarskog.se/error-handling-I.html

cheers,
http://mallier.blogspot.com
Go to Top of Page
   

- Advertisement -