﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server Newbies  / Error handling in while loop...skip if failure / 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, 26 May 2013 02:55:07 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Error handling in while loop...skip if failure</title><link>http://www.sqlservercentral.com/Forums/Topic1403316-1292-1.aspx</link><description>I'm glad I could help :-)</description><pubDate>Mon, 07 Jan 2013 07:29:44 GMT</pubDate><dc:creator>e4d4</dc:creator></item><item><title>RE: Error handling in while loop...skip if failure</title><link>http://www.sqlservercentral.com/Forums/Topic1403316-1292-1.aspx</link><description>Thanks for your reply.The TRY...CATCH block worked so when the stored procedure failed for one program then it skipped to the next one and sent the email. I also used the Error Functions from Try...Catch to print the error no, proc name and error message. Once again thank you for your suggestion.</description><pubDate>Mon, 07 Jan 2013 06:17:21 GMT</pubDate><dc:creator>tudorn</dc:creator></item><item><title>RE: Error handling in while loop...skip if failure</title><link>http://www.sqlservercentral.com/Forums/Topic1403316-1292-1.aspx</link><description>Remove [b]set XACT_ABORT ON[/b] and implement [b]TRY CATCH[/b] [url]http://msdn.microsoft.com/en-us/library/ms175976%28v=sql.100%29.aspx[/url], so this proc should looks like (I can't test it so first test it):[code="sql"]CREATE PROCEDURE [dbo].[proc_Master_Schedule] 	                    AS  -- set XACT_ABORT ON --line to removeDECLARE @Count INTDECLARE	@LoopCount INTDECLARE	@ProgramID INTDECLARE @strMessage NVARCHAR(MAX)DECLARE @subject NVARCHAR(MAX)SELECT IDENTITY(INT,1,1) ID, [ProgramID] INTO #tmpProgram 	FROM 	dbo.Schedule_Setup  WITH (NOLOCK)	WHERE	Deleted=0SELECT @Count = @@RowCountSET @LoopCount = 1WHILE @LoopCount &amp;lt;= @CountBEGIN    SELECT @ProgramID=ProgramID	FROM #tmpProgram 	WHERE ID = @LoopCount                --TRY CATCH block        BEGIN TRY		EXEC dbo.proc_Schedules @ProgramID        END TRY        BEGIN CATCH		SET @Message = 'The Schedule for Program ' + CONVERT(NVARCHAR,@ProgramID) + 'failed.'		exec Master.dbo.sp_sendSAM				@recipients='Dev.Team@test.com',				@subject='proc_Master_Schedule failure',				@sender_email = 'SQL2008@test.com',				@Message = @Message,				@format ='html'        END CATCH;        --end of TRY CATCH block		SET @LoopCount=@LoopCount + 1ENDDROP TABLE #tmpProgram[/code]</description><pubDate>Sun, 06 Jan 2013 03:57:41 GMT</pubDate><dc:creator>e4d4</dc:creator></item><item><title>Error handling in while loop...skip if failure</title><link>http://www.sqlservercentral.com/Forums/Topic1403316-1292-1.aspx</link><description>Hello,I have the following stored procedure that runs as a job: [code="sql"]CREATE PROCEDURE [dbo].[proc_Master_Schedule] 	                    AS  set XACT_ABORT ONDECLARE @Count INTDECLARE	@LoopCount INTDECLARE	@ProgramID INTDECLARE @strMessage NVARCHAR(MAX)DECLARE @subject NVARCHAR(MAX)SELECT IDENTITY(INT,1,1) ID, [ProgramID] INTO #tmpProgram 	FROM 	dbo.Schedule_Setup  WITH (NOLOCK)	WHERE	Deleted=0SELECT @Count = @@RowCountSET @LoopCount = 1WHILE @LoopCount &amp;lt;= @CountBEGIN    SELECT @ProgramID=ProgramID	FROM #tmpProgram 	WHERE ID = @LoopCount		EXEC dbo.proc_Schedules @ProgramID	IF @@Error &amp;lt;&amp;gt; 0		SET @Message = 'The Schedule for Program ' + CONVERT(NVARCHAR,@ProgramID) + 'failed.'		exec Master.dbo.sp_sendSAM				@recipients='Dev.Team@test.com',				@subject='proc_Master_Schedule failure',				@sender_email = 'SQL2008@test.com',				@Message = @Message,				@format ='html'		END	SET @LoopCount=@LoopCount + 1ENDDROP TABLE #tmpProgram[/code]I would like the job to continue to run when there is a failure in executing the stored procedure for any of the ProgramIDs and also send an email that it failed for that specific ProgramID. So should the procedure fail for one ProgramID, then skip to the next ProgramID and notify. Also I need to have better error handling.Any help with regards to this matter will be greatly appreciated.Thank you.</description><pubDate>Sun, 06 Jan 2013 02:14:06 GMT</pubDate><dc:creator>tudorn</dc:creator></item></channel></rss>