Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TRY/CATCH Help Needed

Status
Not open for further replies.

SgtJarrow

Programmer
Apr 12, 2002
2,937
0
0
US
So I have had this process running for some time and thought I had sufficient error handling to catch most of my errors. Over the weekend, I encountered a new error that did not trigger my CATCH block. I am not 100% sure why my CATCH did not report the error and am looking for some help to ensure I catch these types of errors in the future.

My query reaches into a transactionally replicated database on my reporting server. Friday night, the publishing server was upgraded from SQL 2008 R2 to SQL 2014. The subscriber is still on SQL 2008 R2. I don't think that is the issue but wanted to provide that bit of detail. During the upgrade, several tables were excluded from the replication set. So when my query tried to use one of these (now) missing tables, the following error was returned:

Msg 4413, Level 16, State 1, Procedure LoadLSODSTables, Line 533
Could not use view or function 'CV_Prod.dbo.adTeacher' because of binding errors. "

The replication issues has since been resolved and I can now execute the query as needed. What I am needing some help with is shoring up my TRY/CATCH to catch another instance of this in the future. In other words, FORCING my TRY/CATCH to return ANY AND ALL ERRORS, regardless of severity or outcome. My current TRY/CATCH is below (lots removed for clarity but the blocks are in place). CV_Prod is the database that lives on my reporting server but is transactionally replicated from the production database for reporting purposes.

Code:
        BEGIN TRY
	--OMITTED CODE

	WITH cteInstructors(AdClassSchedId, PrimaryInstructor, SecondaryInstructor) AS
	(
	    SELECT
                cs.AdClassSchedID 'AdClassSchedId',
                spi.LastName + ', ' + spi.FirstName 'PrimaryInstructor',
                t.descrip 'SecondaryInstructor'
            FROM CV_Prod.dbo.AdClassSched cs WITH(NOLOCK)
            LEFT JOIN CV_Prod.dbo.SyStaff spi
                    ON cs.AdTeacherID = spi.SyStaffID
            LEFT JOIN CV_Prod.dbo.AdClassSchedInstructor csi
                    ON cs.AdClassSchedID = csi.AdClassSchedID
            LEFT JOIN CV_Prod.dbo.adTeacher t
                    ON csi.AdTeacherID = t.adTeacherID
        )
	SELECT
	    i.AdClassSchedId,
            i.PrimaryInstructor,
            i.SecondaryInstructor
	FROM cteInstructors i;

	EXEC LS_ODS.AddODSLoadLog 'Loaded Courses Working Table', 0;

	--OMITTED CODE
	END TRY
	BEGIN CATCH
		--************
		--NEED THIS CATCH BLOCK TO CATCH ANY AND ALL ERRORS, NO MATTER WHAT SEVERITY OR ERROR NUMBER.
		--************
		EXEC LS_ODS.AddODSLoadLog 'An Error Occurred While Processing The Current GradeExtract File Data', 1;
		--OMITTED CODE
		EXEC msdb.dbo.sp_send_dbmail @profile_name = @EmailProfile, @recipients = @EmailRecipients, @importance = 'High', @subject = @EmailSubject, @body = @EmailBody, @body_format = @EmailFormat;
		RETURN;
	END CATCH

If anyone has any resources to helping me ensure I catch all errors for my process, I would appreciate it. Just need some pointers to find the right answer to my problem and I am not finding the right search terms... Thank you.

Robert "Wizard" Johnson III
U.S. Military Vets MC
CSM, CSPO, MCPD, CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
Senior Database Developer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top