﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Amit Jethva / Article Discussions / Article Discussions by Author  / Capturing The Error Description In A Stored Procedure / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 19:52:18 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>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</description><pubDate>Tue, 13 Jan 2009 03:02:37 GMT</pubDate><dc:creator>Lasse Schioettz</dc:creator></item><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>Thanks AmitUsing 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]GOINSERT INTO authorsvalues('172-32-1176','Doe','Jone','408-496-7223','Bigge Rd.','Menlo Park','CA','94345',1)DECLARE @retval INTEXEC usp_Get_ErrorMessage @retval  outputResults:[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 .sqlBest of LuckLogician --&amp;gt; [Tahir]</description><pubDate>Fri, 07 Nov 2008 13:34:06 GMT</pubDate><dc:creator>tmahmood</dc:creator></item><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>Hi AmitI 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.comRegards</description><pubDate>Thu, 30 Oct 2008 10:31:20 GMT</pubDate><dc:creator>tmahmood</dc:creator></item><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>Another alternative follows.  Note that the error message contains details such asthe 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 araiserror and a print of the error message - in some cases you may want to raise theerror within a stored procedure, while at other times you may want to handle itoutside the SP, such as the print statement that could have just as easily handedthe error message to your calling code or to a logging process.[code][size="2"][font="Courier New"]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]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO----------------------------------------------------------------------Purpose:		Error handling example --Changes:							  --			Bill Nye	4/29/08	Created			  --------------------------------------------------------------------CREATE PROCEDURE [dbo].[usp_errorhandling] 	@Branch int ,	@Division int ,	@errormsg varchar(1000) outputASSET NOCOUNT ONDECLARE @errorid int Select 1/0				-- Purposely cause errorSet @errorid=@@Error	IF @errorid &amp;lt;&amp;gt; 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 OUTPUTPrint @errormsgPrint @rcGODROP PROCEDURE [dbo].[usp_errorhandling]	-- Cleanup[/font][/size][/code]</description><pubDate>Tue, 29 Apr 2008 12:19:29 GMT</pubDate><dc:creator>Bill Nye</dc:creator></item><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>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.comThanks</description><pubDate>Mon, 19 Nov 2007 06:40:30 GMT</pubDate><dc:creator>jbomma</dc:creator></item><item><title>RE: Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>&lt;P&gt;To catch those elusive error messages you could also try:&lt;/P&gt;&lt;P&gt;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&lt;/P&gt;&lt;P&gt;and do whatever you want with the response &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;</description><pubDate>Wed, 27 Apr 2005 17:24:00 GMT</pubDate><dc:creator>Peter E. Kierstead</dc:creator></item><item><title>Capturing The Error Description In A Stored Procedure</title><link>http://www.sqlservercentral.com/Forums/Topic112659-171-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/ajethva/capturingtheerrordescriptioninastoredprocedure.asp&gt;http://www.sqlservercentral.</description><pubDate>Thu, 22 Apr 2004 14:48:00 GMT</pubDate><dc:creator>Amit Jethva</dc:creator></item></channel></rss>