SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Error handling in while loop...skip if failure
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

tudorn
Starting Member

4 Posts

Posted - 01/06/2013 :  04:23:27  Show Profile  Reply with Quote
Hello,

I have the following stored procedure that runs as a job:


CREATE PROCEDURE [dbo].[proc_Master_Schedule] 	
                    
AS
  

set XACT_ABORT ON

DECLARE @Count INT
DECLARE	@LoopCount INT
DECLARE	@ProgramID INT
DECLARE @strMessage NVARCHAR(MAX)
DECLARE @subject NVARCHAR(MAX)


SELECT IDENTITY(INT,1,1) ID, [ProgramID] INTO #tmpProgram 
	FROM 	dbo.Schedule_Setup  WITH (NOLOCK)
	WHERE	Deleted=0

SELECT @Count = @@RowCount
SET @LoopCount = 1

WHILE @LoopCount <= @Count
BEGIN
    SELECT @ProgramID=ProgramID
	FROM #tmpProgram 
	WHERE ID = @LoopCount

		EXEC dbo.proc_Schedules @ProgramID

	IF @@Error <> 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 + 1
END

DROP TABLE #tmpProgram


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.

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/06/2013 :  10:06:22  Show Profile  Reply with Quote
For that you need to alter logic to add failed IDs to a table and at the end of job sent mail for those ProgramIDs by using another loop.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

tudorn
Starting Member

4 Posts

Posted - 01/07/2013 :  08:10:40  Show Profile  Reply with Quote
Thanks for your reply Visakh.
I ended up using TRY...CATCH block.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52249 Posts

Posted - 01/07/2013 :  10:13:36  Show Profile  Reply with Quote
ok...and inside CATCH you captured the ids?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000