SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Capturing The Error Description In A Stored Procedure


Capturing The Error Description In A Stored Procedure

Author
Message
Amit Jethva
Amit Jethva
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1351 Visits: 52
Comments posted to this topic are about the content posted at http://www.sqlservercentral.




--
Amit



"There is no 'patch' for stupidity."



Download the Updated SQL Server 2005 Books Online.
Peter E. Kierstead
Peter E. Kierstead
SSC Eights!
SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)SSC Eights! (874 reputation)

Group: General Forum Members
Points: 874 Visits: 453

To catch those elusive error messages you could also try:

Declare @SQL nVarChar(4000)
Set @SQL=N'osql -Usa -P -Q"declare @t table(PKey Int not Null) insert @t select Null"'
Exec xp_CmdShell @SQL

and do whatever you want with the response





PeteK
I have CDO. It's like OCD but all the letters are in alphabetical order... as they should be.
jbomma
jbomma
Valued Member
Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)Valued Member (58 reputation)

Group: General Forum Members
Points: 58 Visits: 103
Amit, excellent posting!!!!

I am in same kind of situation.. by setting Xact_abort off you are loosing transaction control.

BTW, I could not able to find your code snippet.

Can you please forward to me at my email.... jagadeeswar.bomma@gmail.com

Thanks


jay
Bill Nye
Bill Nye
Right there with Babe
Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)Right there with Babe (778 reputation)

Group: General Forum Members
Points: 778 Visits: 177
Another alternative follows. Note that the error message contains details such as
the parameter values at the time of error and the name of the stored procedure
(or you could refer to a section of SP). You might also consider adding details
like the userid.

The point of this is to show that you CAN get error details out of a stored procedure,
even though the default return value is only an INT datatype. The example shows a
raiserror and a print of the error message - in some cases you may want to raise the
error within a stored procedure, while at other times you may want to handle it
outside the SP, such as the print statement that could have just as easily handed
the error message to your calling code or to a logging process.



IF EXISTS (SELECT * FROM dbo.sysobjects
WHERE id = OBJECT_ID(N'[dbo].[usp_errorhandling]')
AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE [dbo].[usp_errorhandling]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--------------------------------------------------------------------
--Purpose: Error handling example
--Changes:
-- Bill Nye 4/29/08 Created
--------------------------------------------------------------------
CREATE PROCEDURE [dbo].[usp_errorhandling]
@Branch int ,
@Division int ,
@errormsg varchar(1000) output
AS
SET NOCOUNT ON
DECLARE @errorid int

Select 1/0 -- Purposely cause error
Set @errorid=@@Error
IF @errorid <> 0
BEGIN
SELECT @errormsg=description FROM master.dbo.sysmessages
WHERE error=@errorid
SET @errormsg='Error updating when Division_ID='
+ Cast(@Division as char(1)) + ' and Branch_ID='
+ Cast(@Branch as varchar(3)) + ' in usp_errorhandling: '
+ @errormsg
RAISERROR(@errormsg,16,1)
END

GO
-------------------------------

DECLARE @RC Int, @errormsg varchar(1000)
EXEC @RC = [dbo].[usp_errorhandling] 999, 9, @errormsg OUTPUT
Print @errormsg
Print @rc

GO

DROP PROCEDURE [dbo].[usp_errorhandling] -- Cleanup


tmahmood
tmahmood
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 10
Hi Amit

I tried to download your code attached with this topic, But it doesn't work.
Will you please check it and email me your code, that will be great.
tahirmd@hotmail.com

Regards
tmahmood
tmahmood
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 10
Thanks Amit

Using your Idea I'm able to solve "Capturing The Error Description In A Stored..." for SQL 2000.

I would like to share my investigation with you guys.

Here are my findings:-
With this query I'm trying to create duplicate records in pubs.dbo.authors. By calling SP I get SQL error message.

USE [PUBS]
GO

INSERT INTO authors
values('172-32-1176','Doe','Jone','408-496-7223','Bigge Rd.','Menlo Park','CA','94345',1)

DECLARE @retval INT
EXEC usp_Get_ErrorMessage @retval output

Results:
[errNumber: 2627] [errState: 1] [errLevel: 14] [errInstance: MyLocal] [errLine: 1] [errProcedure: NULL] [errMessage: Violation of PRIMARY KEY constraint 'UPKCL_auidind'. Cannot insert duplicate key in object 'authors'.]

(1 row(s) affected)

Note:- Store procedure "usp_Get_ErrorMessage" is attached with this thread. rename .txt with .sql

Best of Luck
Logician --> [Tahir]
Attachments
usp_Get_ErrorMessage.txt (60 views, 9.00 KB)
Lasse Schioettz
Lasse Schioettz
Mr or Mrs. 500
Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)Mr or Mrs. 500 (503 reputation)

Group: General Forum Members
Points: 503 Visits: 54
Bill,

Many of the sysmessage strings contain placeholders that should be fed variables run-time. Extracting the string only will produce messages like this:

"Cannot insert the value NULL into column '%.*ls', table '%.*ls'; column does not allow nulls. %ls fails."

Essential information about which column and which table is missing.

Sincerely,
Lasse
Ryan Henning
Ryan Henning
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 15
Thanks Pete!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search