<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: SQL SERVER &#8211; 2005 &#8211; Explanation of TRY&#8230;CATCH and ERROR Handling</title>
	<atom:link href="http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/</link>
	<description>Personal Notes of Pinal Dave</description>
	<lastBuildDate>Sun, 12 Feb 2012 09:22:39 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: MyDoggieJessie</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-197984</link>
		<dc:creator><![CDATA[MyDoggieJessie]]></dc:creator>
		<pubDate>Thu, 17 Nov 2011 21:37:15 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-197984</guid>
		<description><![CDATA[The best way to test for a linked server is to use SQL&#039;s built in system procedure:

Ex: EXEC sys.sp_testlinkedserver &#039;yourlinkedserver&#039;]]></description>
		<content:encoded><![CDATA[<p>The best way to test for a linked server is to use SQL&#8217;s built in system procedure:</p>
<p>Ex: EXEC sys.sp_testlinkedserver &#8216;yourlinkedserver&#8217;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sunil Somani</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-184990</link>
		<dc:creator><![CDATA[Sunil Somani]]></dc:creator>
		<pubDate>Sat, 29 Oct 2011 13:13:16 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-184990</guid>
		<description><![CDATA[which one we should use transaction/@@error or try/catch.]]></description>
		<content:encoded><![CDATA[<p>which one we should use transaction/@@error or try/catch.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-150054</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Fri, 22 Jul 2011 14:53:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-150054</guid>
		<description><![CDATA[Yes. But it is not possible without dynamic sql]]></description>
		<content:encoded><![CDATA[<p>Yes. But it is not possible without dynamic sql</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prabbu</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149712</link>
		<dc:creator><![CDATA[Prabbu]]></dc:creator>
		<pubDate>Thu, 21 Jul 2011 05:22:49 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149712</guid>
		<description><![CDATA[ALTER PROC SAMPLE1
AS
BEGIN
	BEGIN TRY
		PRINT &#039;1&#039;
		DECLARE @SQL VARCHAR(MAX)
		SET @SQL = &#039;insert Permanenttable select * from Temporarytable&#039;
		EXEC(@SQL)
--		select 1/0
		PRINT &#039;2&#039;
	END TRY
	BEGIN CATCH
		print &#039;catch fired&#039;
		 SELECT 
        ERROR_NUMBER() AS ErrorNumber,
        ERROR_SEVERITY() AS ErrorSeverity,
        ERROR_STATE() as ErrorState,
        ERROR_PROCEDURE() as ErrorProcedure,
        ERROR_LINE() as ErrorLine,
        ERROR_MESSAGE() as ErrorMessage;   
	END CATCH
END]]></description>
		<content:encoded><![CDATA[<p>ALTER PROC SAMPLE1<br />
AS<br />
BEGIN<br />
	BEGIN TRY<br />
		PRINT &#8217;1&#8242;<br />
		DECLARE @SQL VARCHAR(MAX)<br />
		SET @SQL = &#8216;insert Permanenttable select * from Temporarytable&#8217;<br />
		EXEC(@SQL)<br />
&#8211;		select 1/0<br />
		PRINT &#8217;2&#8242;<br />
	END TRY<br />
	BEGIN CATCH<br />
		print &#8216;catch fired&#8217;<br />
		 SELECT<br />
        ERROR_NUMBER() AS ErrorNumber,<br />
        ERROR_SEVERITY() AS ErrorSeverity,<br />
        ERROR_STATE() as ErrorState,<br />
        ERROR_PROCEDURE() as ErrorProcedure,<br />
        ERROR_LINE() as ErrorLine,<br />
        ERROR_MESSAGE() as ErrorMessage;<br />
	END CATCH<br />
END</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shaik Nazeer Hussain</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149711</link>
		<dc:creator><![CDATA[Shaik Nazeer Hussain]]></dc:creator>
		<pubDate>Thu, 21 Jul 2011 05:22:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149711</guid>
		<description><![CDATA[I understand try catch blocks having some limitation.
But how can i handle this type of exception?
I am having scenario like following

Create procedure sp1
as 
Begin
     
     Begin Try
     Begin Tran
            
            Declare cursor1 cursor for ............
            ..................
            While @@Fetch_status=0
            Begin

                       Declare cursor2 cursor for ............
                       ..................
                       While @@Fetch_status=0
                       Begin

                                    Declare cursor3 cursor for ............
                                    ..................
                                    While @@Fetch_status=0
                                     Begin

                                                    insert Permanenttable
                                                    select * from Temporarytable
                                      End
                      End

               End

               IF @@TRANCOUNT &gt; 0     
	COMMIT TRAN
    End Try

    Begin catch

           Rollback tran
                  
          IF CURSOR_STATUS(&#039;GLOBAL&#039;, &#039;Cursor1&#039;) &gt;= 0 
              BEGIN 
                CLOSE Cursor1
                DEALLOCATE Cursor1
               END 
            IF CURSOR_STATUS(&#039;GLOBAL&#039;, &#039;Cursor2&#039;) &gt;= 0 
              BEGIN 
                CLOSE Cursor2
                DEALLOCATE Cursor2
               END 

            IF CURSOR_STATUS(&#039;GLOBAL&#039;, &#039;Cursor3&#039;) &gt;= 0 
              BEGIN 
                CLOSE Cursor3
                DEALLOCATE Cursor3
               END 
End catch

END


If exception raised at the time of inserting then it will terminate.But my all cursors will be open state only.

If next time i execute this SP it will give Cursor1 already exist and transaction opened error.

So can any one tell me how can i acheive this type of scenario.]]></description>
		<content:encoded><![CDATA[<p>I understand try catch blocks having some limitation.<br />
But how can i handle this type of exception?<br />
I am having scenario like following</p>
<p>Create procedure sp1<br />
as<br />
Begin</p>
<p>     Begin Try<br />
     Begin Tran</p>
<p>            Declare cursor1 cursor for &#8230;&#8230;&#8230;&#8230;<br />
            &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
            While @@Fetch_status=0<br />
            Begin</p>
<p>                       Declare cursor2 cursor for &#8230;&#8230;&#8230;&#8230;<br />
                       &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
                       While @@Fetch_status=0<br />
                       Begin</p>
<p>                                    Declare cursor3 cursor for &#8230;&#8230;&#8230;&#8230;<br />
                                    &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;<br />
                                    While @@Fetch_status=0<br />
                                     Begin</p>
<p>                                                    insert Permanenttable<br />
                                                    select * from Temporarytable<br />
                                      End<br />
                      End</p>
<p>               End</p>
<p>               IF @@TRANCOUNT &gt; 0<br />
	COMMIT TRAN<br />
    End Try</p>
<p>    Begin catch</p>
<p>           Rollback tran</p>
<p>          IF CURSOR_STATUS(&#8216;GLOBAL&#8217;, &#8216;Cursor1&#8242;) &gt;= 0<br />
              BEGIN<br />
                CLOSE Cursor1<br />
                DEALLOCATE Cursor1<br />
               END<br />
            IF CURSOR_STATUS(&#8216;GLOBAL&#8217;, &#8216;Cursor2&#8242;) &gt;= 0<br />
              BEGIN<br />
                CLOSE Cursor2<br />
                DEALLOCATE Cursor2<br />
               END </p>
<p>            IF CURSOR_STATUS(&#8216;GLOBAL&#8217;, &#8216;Cursor3&#8242;) &gt;= 0<br />
              BEGIN<br />
                CLOSE Cursor3<br />
                DEALLOCATE Cursor3<br />
               END<br />
End catch</p>
<p>END</p>
<p>If exception raised at the time of inserting then it will terminate.But my all cursors will be open state only.</p>
<p>If next time i execute this SP it will give Cursor1 already exist and transaction opened error.</p>
<p>So can any one tell me how can i acheive this type of scenario.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: madhivanan</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149601</link>
		<dc:creator><![CDATA[madhivanan]]></dc:creator>
		<pubDate>Wed, 20 Jul 2011 13:37:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149601</guid>
		<description><![CDATA[The invalid object name error cannot be jhandled by CATCH block]]></description>
		<content:encoded><![CDATA[<p>The invalid object name error cannot be jhandled by CATCH block</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Shaik Nazeer Hussain</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149575</link>
		<dc:creator><![CDATA[Shaik Nazeer Hussain]]></dc:creator>
		<pubDate>Wed, 20 Jul 2011 10:45:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-149575</guid>
		<description><![CDATA[Hi,

I am using sqlserver 2005.if i execute following lines of code in sqlserver catch bolck not executing.Since i dont have Permanenttable and Temporarytable in my db.

BEGIN TRY 
insert Permanenttable
select * from Temporarytable
END TRY 
BEGIN CATCH
print &#039;catch fired&#039;
END CATCH]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I am using sqlserver 2005.if i execute following lines of code in sqlserver catch bolck not executing.Since i dont have Permanenttable and Temporarytable in my db.</p>
<p>BEGIN TRY<br />
insert Permanenttable<br />
select * from Temporarytable<br />
END TRY<br />
BEGIN CATCH<br />
print &#8216;catch fired&#8217;<br />
END CATCH</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: obperryo</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-93052</link>
		<dc:creator><![CDATA[obperryo]]></dc:creator>
		<pubDate>Thu, 14 Oct 2010 14:29:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-93052</guid>
		<description><![CDATA[I understand the Try Catch, but how can I catch that the link server is down, and test it without taking down the link server?  

BEGIN TRY

	--SELECT 1/0
	SELECT state_desc FROM LinkedServerName.master.sys.databases WHERE [Name] = &#039;DBaseName&#039;

END TRY
BEGIN CATCH


SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;

END CATCH]]></description>
		<content:encoded><![CDATA[<p>I understand the Try Catch, but how can I catch that the link server is down, and test it without taking down the link server?  </p>
<p>BEGIN TRY</p>
<p>	&#8211;SELECT 1/0<br />
	SELECT state_desc FROM LinkedServerName.master.sys.databases WHERE [Name] = &#8216;DBaseName&#8217;</p>
<p>END TRY<br />
BEGIN CATCH</p>
<p>SELECT<br />
ERROR_NUMBER() AS ErrorNumber,<br />
ERROR_SEVERITY() AS ErrorSeverity,<br />
ERROR_STATE() AS ErrorState,<br />
ERROR_PROCEDURE() AS ErrorProcedure,<br />
ERROR_LINE() AS ErrorLine,<br />
ERROR_MESSAGE() AS ErrorMessage;</p>
<p>END CATCH</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sandipak Bagchi</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-89161</link>
		<dc:creator><![CDATA[Sandipak Bagchi]]></dc:creator>
		<pubDate>Thu, 23 Sep 2010 21:29:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-89161</guid>
		<description><![CDATA[Pinal,

Can you please provide some more info on NESTED TRY CATCH blocks and how the control passes from one to another during calling one stored procedure from another.

I have the following situation


CREATE PROCEDURE sp_a(
....)
AS
BEGIN...
..
BEGIN TRY   -- Outer Try block
   UPDATE TABLE a.....WHERE ...


EXEC sp_b

  UPDATE TABLE....
  SET a.....
  WHERE....

END TRY 
BEGIN CATCH

   SET @ErrorMsg = &#039;ErrNo: &#039; + ERROR_NUMBER() + &#039; Msg: &#039; +                    
                                ERROR_MESSAGE()

END CATCH

END

---- End of Stored Proc - sp_a

CREATE PROCEDURE sp_b(..
...)
...

BEGIN TRY  --- Nested Try Block - Inner block
     INSERT INTO TABLE.....
     VALUES(...)
END TRY
BEGIN CATCH

    SET @ErrorMsg = &#039;ErrNo: &#039; + ERROR_NUMBER() + &#039; Msg: &#039; +                    
                                ERROR_MESSAGE()
END CATCH

END  --- 

---- End of Stored Proc sp_b

So in this case if the sproc sp_b returns error, does the stored procedure capture that error and goes to the Outer CATCH block or do I have log the Inner CATCH error separately into a table.


Any feedback would be much appreciated]]></description>
		<content:encoded><![CDATA[<p>Pinal,</p>
<p>Can you please provide some more info on NESTED TRY CATCH blocks and how the control passes from one to another during calling one stored procedure from another.</p>
<p>I have the following situation</p>
<p>CREATE PROCEDURE sp_a(<br />
&#8230;.)<br />
AS<br />
BEGIN&#8230;<br />
..<br />
BEGIN TRY   &#8212; Outer Try block<br />
   UPDATE TABLE a&#8230;..WHERE &#8230;</p>
<p>EXEC sp_b</p>
<p>  UPDATE TABLE&#8230;.<br />
  SET a&#8230;..<br />
  WHERE&#8230;.</p>
<p>END TRY<br />
BEGIN CATCH</p>
<p>   SET @ErrorMsg = &#8216;ErrNo: &#8216; + ERROR_NUMBER() + &#8216; Msg: &#8216; +<br />
                                ERROR_MESSAGE()</p>
<p>END CATCH</p>
<p>END</p>
<p>&#8212;- End of Stored Proc &#8211; sp_a</p>
<p>CREATE PROCEDURE sp_b(..<br />
&#8230;)<br />
&#8230;</p>
<p>BEGIN TRY  &#8212; Nested Try Block &#8211; Inner block<br />
     INSERT INTO TABLE&#8230;..<br />
     VALUES(&#8230;)<br />
END TRY<br />
BEGIN CATCH</p>
<p>    SET @ErrorMsg = &#8216;ErrNo: &#8216; + ERROR_NUMBER() + &#8216; Msg: &#8216; +<br />
                                ERROR_MESSAGE()<br />
END CATCH</p>
<p>END  &#8212; </p>
<p>&#8212;- End of Stored Proc sp_b</p>
<p>So in this case if the sproc sp_b returns error, does the stored procedure capture that error and goes to the Outer CATCH block or do I have log the Inner CATCH error separately into a table.</p>
<p>Any feedback would be much appreciated</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Prashant Goyal</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-82308</link>
		<dc:creator><![CDATA[Prashant Goyal]]></dc:creator>
		<pubDate>Fri, 30 Jul 2010 06:26:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-82308</guid>
		<description><![CDATA[i want to know in sql script what type of
exception are generate like FOREGIN KEY,PRIMARY KEY, UNIQUE KEY VIOLATION 
so that i take a step to generate a appropriate message to
user that it understand what is error.]]></description>
		<content:encoded><![CDATA[<p>i want to know in sql script what type of<br />
exception are generate like FOREGIN KEY,PRIMARY KEY, UNIQUE KEY VIOLATION<br />
so that i take a step to generate a appropriate message to<br />
user that it understand what is error.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brad</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-81420</link>
		<dc:creator><![CDATA[Brad]]></dc:creator>
		<pubDate>Fri, 23 Jul 2010 15:18:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-81420</guid>
		<description><![CDATA[Ryan did you ever get an answer to your question.  I am having a similar issue.]]></description>
		<content:encoded><![CDATA[<p>Ryan did you ever get an answer to your question.  I am having a similar issue.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Freeman</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-75104</link>
		<dc:creator><![CDATA[Mark Freeman]]></dc:creator>
		<pubDate>Mon, 07 Jun 2010 19:21:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-75104</guid>
		<description><![CDATA[I have a stored procedure that updates a linked server. The linked server is returning an error (because the update would violate a business rule). 

ERROR_MESSAGE() has &#039;Cannot fetch a row from OLE DB provider &quot;&quot; for linked server &quot;&quot;.&#039;

This is OK as far as it goes. But when I debug this in VS 2008, the Debug Window displays this much more detailed info:

OLE DB provider &quot;&quot; for linked server &quot;&quot; returned message &quot;Cursor fetch row failed.
exception 98
VER2021-Service Records can not overlap for DSP.&quot;.

How can I access that more detailed message within my stored procedure&#039;s CATCH block?]]></description>
		<content:encoded><![CDATA[<p>I have a stored procedure that updates a linked server. The linked server is returning an error (because the update would violate a business rule). </p>
<p>ERROR_MESSAGE() has &#8216;Cannot fetch a row from OLE DB provider &#8220;&#8221; for linked server &#8220;&#8221;.&#8217;</p>
<p>This is OK as far as it goes. But when I debug this in VS 2008, the Debug Window displays this much more detailed info:</p>
<p>OLE DB provider &#8220;&#8221; for linked server &#8220;&#8221; returned message &#8220;Cursor fetch row failed.<br />
exception 98<br />
VER2021-Service Records can not overlap for DSP.&#8221;.</p>
<p>How can I access that more detailed message within my stored procedure&#8217;s CATCH block?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ks</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-74874</link>
		<dc:creator><![CDATA[ks]]></dc:creator>
		<pubDate>Sat, 05 Jun 2010 14:07:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-74874</guid>
		<description><![CDATA[Is it possible for try..catch to be global for some reason? We are seeing stored procedures calling an error statement even though there is no error checking (try catch or otherwise) on that particular statement. For example, in the sp we might do inserts and on those we check @@error and we always do an insert into table called errors if we see a failure, but we have some update statements that we expect to not find anything to update in certain conditions so we do not check for any errors yet when they do not work we are getting an error on not being able to insert into the table errors and the sp bombs.  Is it possible for there to be a global try catch that gets called somehow?]]></description>
		<content:encoded><![CDATA[<p>Is it possible for try..catch to be global for some reason? We are seeing stored procedures calling an error statement even though there is no error checking (try catch or otherwise) on that particular statement. For example, in the sp we might do inserts and on those we check @@error and we always do an insert into table called errors if we see a failure, but we have some update statements that we expect to not find anything to update in certain conditions so we do not check for any errors yet when they do not work we are getting an error on not being able to insert into the table errors and the sp bombs.  Is it possible for there to be a global try catch that gets called somehow?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Geervani</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-66629</link>
		<dc:creator><![CDATA[Geervani]]></dc:creator>
		<pubDate>Sat, 24 Apr 2010 12:35:07 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-66629</guid>
		<description><![CDATA[Hello Sir,

Is there any way to catch error with severity less than 10.
I have a Stored Proc wherein dynamic sql query is generated. If the query is wrong, How can i catch the exception?

If the query generated can be wrong, than the user input is wrong and hence i need to update another table.

Can u pls help .. 

Thanks 
Geervani]]></description>
		<content:encoded><![CDATA[<p>Hello Sir,</p>
<p>Is there any way to catch error with severity less than 10.<br />
I have a Stored Proc wherein dynamic sql query is generated. If the query is wrong, How can i catch the exception?</p>
<p>If the query generated can be wrong, than the user input is wrong and hence i need to update another table.</p>
<p>Can u pls help .. </p>
<p>Thanks<br />
Geervani</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sandra</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-66165</link>
		<dc:creator><![CDATA[Sandra]]></dc:creator>
		<pubDate>Thu, 22 Apr 2010 08:56:47 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-66165</guid>
		<description><![CDATA[Hi , 

kindly let me know how to capture the exception raised by the below sql and i want to use insert Try and Catch in the sql below.

DECLARE @triprequest varchar (50)
DECLARE @xml_str varchar(4000)
DECLARE @hdoc int
declare  @temp_table_travel_Req_xml_det table ([triprequest] varchar (50),[name] varchar (50), [created_by] varchar (50), [action] varchar (16), [created_on] datetime)
DECLARE xml_cur cursor for
SELECT CMTRAVELCOMMENTS.tripRequest triprequest, cast(cast(CMTRAVELCOMMENTS.config as
varbinary(8000)) as VARCHAR(8000)) xml_str
FROM CMTRAVELCOMMENTS, PCMTRAVELREQUEST
WHERE
PCMTRAVELREQUEST.NODESTATE = &#039;Approved request&#039;
and CMTRAVELCOMMENTS.tripRequest=PCMTRAVELREQUEST.tripRequest
OPEN xml_cur
FETCH xml_cur into @triprequest,@xml_str
WHILE(@@fetch_status=0)
BEGIN
EXEC sp_xml_preparedocument @hdoc OUTPUT, @xml_str 
    INSERT into temp_table_travel_Req_xml_det
    SELECT @triprequest,*
    FROM OPENXML (@hdoc,&#039;/cm-comments/comments&#039;, 3)
        WITH ([name] varchar (50), [created_by] varchar (50), [action] varchar (16), [created_on] datetime) 
    EXEC sp_xml_removedocument @hdoc    
	print @triprequest 
FETCH xml_cur into @triprequest,@xml_str
END
CLOSE xml_cur
DEALLOCATE xml_cur]]></description>
		<content:encoded><![CDATA[<p>Hi , </p>
<p>kindly let me know how to capture the exception raised by the below sql and i want to use insert Try and Catch in the sql below.</p>
<p>DECLARE @triprequest varchar (50)<br />
DECLARE @xml_str varchar(4000)<br />
DECLARE @hdoc int<br />
declare  @temp_table_travel_Req_xml_det table ([triprequest] varchar (50),[name] varchar (50), [created_by] varchar (50), [action] varchar (16), [created_on] datetime)<br />
DECLARE xml_cur cursor for<br />
SELECT CMTRAVELCOMMENTS.tripRequest triprequest, cast(cast(CMTRAVELCOMMENTS.config as<br />
varbinary(8000)) as VARCHAR(8000)) xml_str<br />
FROM CMTRAVELCOMMENTS, PCMTRAVELREQUEST<br />
WHERE<br />
PCMTRAVELREQUEST.NODESTATE = &#8216;Approved request&#8217;<br />
and CMTRAVELCOMMENTS.tripRequest=PCMTRAVELREQUEST.tripRequest<br />
OPEN xml_cur<br />
FETCH xml_cur into @triprequest,@xml_str<br />
WHILE(@@fetch_status=0)<br />
BEGIN<br />
EXEC sp_xml_preparedocument @hdoc OUTPUT, @xml_str<br />
    INSERT into temp_table_travel_Req_xml_det<br />
    SELECT @triprequest,*<br />
    FROM OPENXML (@hdoc,&#8217;/cm-comments/comments&#8217;, 3)<br />
        WITH ([name] varchar (50), [created_by] varchar (50), [action] varchar (16), [created_on] datetime)<br />
    EXEC sp_xml_removedocument @hdoc<br />
	print @triprequest<br />
FETCH xml_cur into @triprequest,@xml_str<br />
END<br />
CLOSE xml_cur<br />
DEALLOCATE xml_cur</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Murugan Killada</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-65997</link>
		<dc:creator><![CDATA[Murugan Killada]]></dc:creator>
		<pubDate>Wed, 21 Apr 2010 18:36:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-65997</guid>
		<description><![CDATA[Check upfront if object exists or not.

If exists(
Select * from information_schema.tables where table_name=
)  ... then go ahead inserting data.]]></description>
		<content:encoded><![CDATA[<p>Check upfront if object exists or not.</p>
<p>If exists(<br />
Select * from information_schema.tables where table_name=<br />
)  &#8230; then go ahead inserting data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Suman Singh</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-65761</link>
		<dc:creator><![CDATA[Suman Singh]]></dc:creator>
		<pubDate>Tue, 20 Apr 2010 19:26:58 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-65761</guid>
		<description><![CDATA[Hi,

I tried to insert data into a table which doesn&#039;t exists from a SP. In this case, I am not able to trap the error in CATCH block and it breaks while running in between.

BEGIN TRY
--Insert statement
END TRY
BEGIN CATCH
PRINT &#039;Error Detected&#039;
END CATCH

Output:
Msg 208, Level 16, State 1, Procedure usp_Calculation, Line 805
Invalid object name &#039;tbl_Allocation_Debug&#039;.]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I tried to insert data into a table which doesn&#8217;t exists from a SP. In this case, I am not able to trap the error in CATCH block and it breaks while running in between.</p>
<p>BEGIN TRY<br />
&#8211;Insert statement<br />
END TRY<br />
BEGIN CATCH<br />
PRINT &#8216;Error Detected&#8217;<br />
END CATCH</p>
<p>Output:<br />
Msg 208, Level 16, State 1, Procedure usp_Calculation, Line 805<br />
Invalid object name &#8216;tbl_Allocation_Debug&#8217;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: parveen kumar</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-64107</link>
		<dc:creator><![CDATA[parveen kumar]]></dc:creator>
		<pubDate>Wed, 31 Mar 2010 09:49:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-64107</guid>
		<description><![CDATA[CAN WE USE TRY CATCH IN FUNCTIONS?]]></description>
		<content:encoded><![CDATA[<p>CAN WE USE TRY CATCH IN FUNCTIONS?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Murugan Killada</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-60293</link>
		<dc:creator><![CDATA[Murugan Killada]]></dc:creator>
		<pubDate>Wed, 27 Jan 2010 20:09:36 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-60293</guid>
		<description><![CDATA[Iam using openrowset  function to load data from text file. 
First row, first field value has carriage return and hence when
openrowset function is executed outside the Try - Catch block
gives the following error.
Msg 4864, Level 16, State 1, Line 1
&quot;Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (column_name). &quot;

When openrowset is executed inside the Try block, works fine
, skips the first row as it had problem with the first column value and inserts from second row onwards.
Basically function inserted all rows excluding the problematic ones, without giving any error.
How can i find those problematic rows, as none of the errors are caught in Catch Block.]]></description>
		<content:encoded><![CDATA[<p>Iam using openrowset  function to load data from text file.<br />
First row, first field value has carriage return and hence when<br />
openrowset function is executed outside the Try &#8211; Catch block<br />
gives the following error.<br />
Msg 4864, Level 16, State 1, Line 1<br />
&#8220;Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 1 (column_name). &#8221;</p>
<p>When openrowset is executed inside the Try block, works fine<br />
, skips the first row as it had problem with the first column value and inserts from second row onwards.<br />
Basically function inserted all rows excluding the problematic ones, without giving any error.<br />
How can i find those problematic rows, as none of the errors are caught in Catch Block.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Saurabh Gupta</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-55430</link>
		<dc:creator><![CDATA[Saurabh Gupta]]></dc:creator>
		<pubDate>Mon, 31 Aug 2009 10:51:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-55430</guid>
		<description><![CDATA[Manisha
What are you trying to print? This print statement will give error. Do you want to concatenate %1 with statemetn in @SQLQUERY.
If yes then Write
PRINT &#039;%1!&#039; + @SQLQUERY
]]></description>
		<content:encoded><![CDATA[<p>Manisha<br />
What are you trying to print? This print statement will give error. Do you want to concatenate %1 with statemetn in @SQLQUERY.<br />
If yes then Write<br />
PRINT &#8216;%1!&#8217; + @SQLQUERY</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: manisha</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-54486</link>
		<dc:creator><![CDATA[manisha]]></dc:creator>
		<pubDate>Wed, 05 Aug 2009 18:32:35 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-54486</guid>
		<description><![CDATA[Hi,

I would like to print the query I have written inside the SP while executing it so that I can debug it easily
e.g

BEGIN        
	DECLARE @SQLQUERY varchar(8000) 
	SET @SQLQUERY = &#039;select * from Roles where Role=1 &#039;  
	PRINT &#039;%1!&#039;, @SQLQUERY 
	EXEC(@SQLQUERY) 
END

I want if I execute it in sql plus it shoul give me results as well as print the query for my knowldege.

The baove syntaxt works fine with sybase but in sql server it gives me error nea comma in print statement.]]></description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>I would like to print the query I have written inside the SP while executing it so that I can debug it easily<br />
e.g</p>
<p>BEGIN<br />
	DECLARE @SQLQUERY varchar(8000)<br />
	SET @SQLQUERY = &#8216;select * from Roles where Role=1 &#8216;<br />
	PRINT &#8216;%1!&#8217;, @SQLQUERY<br />
	EXEC(@SQLQUERY)<br />
END</p>
<p>I want if I execute it in sql plus it shoul give me results as well as print the query for my knowldege.</p>
<p>The baove syntaxt works fine with sybase but in sql server it gives me error nea comma in print statement.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reddy</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-51015</link>
		<dc:creator><![CDATA[Reddy]]></dc:creator>
		<pubDate>Wed, 15 Apr 2009 12:36:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-51015</guid>
		<description><![CDATA[Imran

Thanks for your quick reply.

yes i am using linked server. I implemented sqlmail on my local server and i am getting mails. 
As i mplemented Try Catch in my trigger and we know that not all the errors will be cathed in the catch block. so better i implement the the way you suggested.

How do we check that remote server is online or not, is there any code snippet you have

part1: if (condition to check if remote server database is    online)
begin
perform what ever your action you want to perform.
end

part2: else
begin
xp_sendemail……
end

I am okay with the 2nd part and what should i write for part1? is there any system stored procedure to do that in sql2k5 as i am using sql2k5. Reply will be appreciated.

Thanks in advance.]]></description>
		<content:encoded><![CDATA[<p>Imran</p>
<p>Thanks for your quick reply.</p>
<p>yes i am using linked server. I implemented sqlmail on my local server and i am getting mails.<br />
As i mplemented Try Catch in my trigger and we know that not all the errors will be cathed in the catch block. so better i implement the the way you suggested.</p>
<p>How do we check that remote server is online or not, is there any code snippet you have</p>
<p>part1: if (condition to check if remote server database is    online)<br />
begin<br />
perform what ever your action you want to perform.<br />
end</p>
<p>part2: else<br />
begin<br />
xp_sendemail……<br />
end</p>
<p>I am okay with the 2nd part and what should i write for part1? is there any system stored procedure to do that in sql2k5 as i am using sql2k5. Reply will be appreciated.</p>
<p>Thanks in advance.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: VKP</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-51013</link>
		<dc:creator><![CDATA[VKP]]></dc:creator>
		<pubDate>Wed, 15 Apr 2009 10:57:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-51013</guid>
		<description><![CDATA[Nice one....]]></description>
		<content:encoded><![CDATA[<p>Nice one&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Imran Mohammed</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-50999</link>
		<dc:creator><![CDATA[Imran Mohammed]]></dc:creator>
		<pubDate>Wed, 15 Apr 2009 02:14:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-50999</guid>
		<description><![CDATA[@Reddy, 

Instead of waiting for trigger to execute SQL statement on remote sql server (which is offline) and fail and then send an email... why don&#039;t you simple check if remote server or remote database is online. If it is online perform action, if it not online, then send email. something like this. 

Inside trigger you can add a check like this,

if  (condition to check if remote server database is online)
begin 
perform what ever your action you want to perform.
end

else 
begin
xp_sendemail......
end

This will definitely not rollback your transaction. 

If you need more help let me know. Also provide details if you are using linked server to connect to remote server. 

~ IM.]]></description>
		<content:encoded><![CDATA[<p>@Reddy, </p>
<p>Instead of waiting for trigger to execute SQL statement on remote sql server (which is offline) and fail and then send an email&#8230; why don&#8217;t you simple check if remote server or remote database is online. If it is online perform action, if it not online, then send email. something like this. </p>
<p>Inside trigger you can add a check like this,</p>
<p>if  (condition to check if remote server database is online)<br />
begin<br />
perform what ever your action you want to perform.<br />
end</p>
<p>else<br />
begin<br />
xp_sendemail&#8230;&#8230;<br />
end</p>
<p>This will definitely not rollback your transaction. </p>
<p>If you need more help let me know. Also provide details if you are using linked server to connect to remote server. </p>
<p>~ IM.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Reddy</title>
		<link>http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-50976</link>
		<dc:creator><![CDATA[Reddy]]></dc:creator>
		<pubDate>Tue, 14 Apr 2009 14:46:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.sqlauthority.com/2007/04/11/sql-server-2005-explanation-of-trycatch-and-error-handling/#comment-50976</guid>
		<description><![CDATA[Hi All,

I have a situation in my Trigger.
My trigger is for update on a Table, whenever there is an update it has to fire the trigger and place the data in 2 Servers one is local and another is Remote server. what i need is sometimes my remote server goes offline, thogh the trigger firing and and i am missing some data. what i want is if is there any problem with servers or DB still it has to fire the trigger and it should notify me with a mail that there was a problem with remote server or DB, but still it has to do all the tasks it is suppose to do. so implemented try catch bolck in catch block i wrote like this

END TRY

Begin Catch
SELECT @intErrorCode = @@ERROR
    IF (@intErrorCode  0) GOTO PROBLEM

End Catch	

PROBLEM:
IF (@intErrorCode  0) 
BEGIN
declare @body1 varchar(100)
set @body1 = &#039;Server :&#039;+@@servername+ &#039;Error Message:&#039;+ERROR_MESSAGE()
EXEC msdb.dbo.sp_send_dbmail @recipients=&#039;mailid&#039;,
    @subject = &#039;My Mail Test&#039;,
    @body = @body1,
    @body_format = &#039;HTML&#039; ;
END

while i am trying get some exception trigger is not getting fired i have to rollback my operation.
Is there anyway i canget fired my trigger though there is an exception but notifying me with a mail. Any Help....]]></description>
		<content:encoded><![CDATA[<p>Hi All,</p>
<p>I have a situation in my Trigger.<br />
My trigger is for update on a Table, whenever there is an update it has to fire the trigger and place the data in 2 Servers one is local and another is Remote server. what i need is sometimes my remote server goes offline, thogh the trigger firing and and i am missing some data. what i want is if is there any problem with servers or DB still it has to fire the trigger and it should notify me with a mail that there was a problem with remote server or DB, but still it has to do all the tasks it is suppose to do. so implemented try catch bolck in catch block i wrote like this</p>
<p>END TRY</p>
<p>Begin Catch<br />
SELECT @intErrorCode = @@ERROR<br />
    IF (@intErrorCode  0) GOTO PROBLEM</p>
<p>End Catch	</p>
<p>PROBLEM:<br />
IF (@intErrorCode  0)<br />
BEGIN<br />
declare @body1 varchar(100)<br />
set @body1 = &#8216;Server :&#8217;+@@servername+ &#8216;Error Message:&#8217;+ERROR_MESSAGE()<br />
EXEC msdb.dbo.sp_send_dbmail @recipients=&#8217;mailid&#8217;,<br />
    @subject = &#8216;My Mail Test&#8217;,<br />
    @body = @body1,<br />
    @body_format = &#8216;HTML&#8217; ;<br />
END</p>
<p>while i am trying get some exception trigger is not getting fired i have to rollback my operation.<br />
Is there anyway i canget fired my trigger though there is an exception but notifying me with a mail. Any Help&#8230;.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
