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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SP ERROR HANDLING

Status
Not open for further replies.

KCWMS

MIS
Mar 25, 2004
38
US
Using SQL SERVER 2K
the following is the meat-n-potatoes of an sp. It generates an error because there are related child tables that the DELETE would leave orphaned, but my output parameter is never filled with any message and the RETURN value is 0.

What am I not understanding here?
Code:
        DELETE FROM dbo.tbl2

	SET IDENTITY_INSERT dbo.tbl2 ON
	INSERT INTO dbo.Biller
		(field1,field2)
	
	SELECT 
		c.field1, c.field2
	FROM	dbo.tbl1 c 

	SET IDENTITY_INSERT dbo.Biller OFF

	--if any errors, return description in @ErrorMsg
	IF @@ERROR > 0
	 BEGIN
	  SELECT @ErrorMsg = [DESCRIPTION]
	  FROM master.dbo.sysmessages
	  WHERE error = @@ERROR
	 END
	
	--exit
	RETURN @@ERROR
 
You have to catch the @@error right after the delete command, since @@error is changed for each command!!!

"A long life is when each day is full and every hour wide!"
(A Jewish Mentor)
M. [hourglass]

 
WHat Micha is getting at is your error code needs to look like
Code:
declare @e int
--some code her to cause an error
set @e = @@error
if @e <> 0 
begin
raiserror @e etc....
end
 
Thanks, NoCoolHandler... [pipe]

"A long life is when each day is full and every hour wide!"
(A Jewish Mentor)
M. [hourglass]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top