{"id":112,"date":"2011-01-20T21:20:00","date_gmt":"2011-01-21T03:20:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=112"},"modified":"2023-07-26T12:31:58","modified_gmt":"2023-07-26T17:31:58","slug":"sql-error-handling","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=112","title":{"rendered":"SQL Error Handling"},"content":{"rendered":"<p>During the pre-SQL Server 2005 days, we had to handle errors via transactions.  At times, this could prove challenging when dealing with specific errors.  Some might fall through making debugging difficult.<\/p>\n<p>Fortunately, the team at Microsoft copied the try\/catch approach from Visual Studio to SQL Server.<\/p>\n<p>Here is the deprecated approach:<\/p>\n<p><span style=\"color: rgb(51, 102, 102);font-family:courier new;\" >BEGIN TRANSACTION<\/p>\n<p> DELETE FROM tblBlah<br \/> WHERE DATESENT <= @DATE  IF @@ERROR = 0     COMMIT TRANSACTION ELSE  ROLLBACK TRANSACTION<\/span><\/p>\n<p>Here is the current best practice:<\/p>\n<p><span style=\"color: rgb(51, 102, 102);font-family:courier new;\" >BEGIN TRY<br \/>BEGIN TRANSACTION<\/p>\n<p> DELETE FROM dbo.tblBlah<br \/> WHERE DATESENT <= @DATE  COMMIT TRANSACTION  END TRY  BEGIN CATCH   IF @@TRANCOUNT > 0<br \/>ROLLBACK TRANSACTION<\/p>\n<p>END CATCH<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>During the pre-SQL Server 2005 days, we had to handle errors via transactions. At times, this could prove challenging when dealing with specific errors. Some might fall through making debugging difficult. Fortunately, the team at Microsoft copied the try\/catch approach from Visual Studio to SQL Server. Here is the deprecated approach: BEGIN TRANSACTION DELETE FROM &hellip; <a href=\"https:\/\/www.chrystalsander.com\/blog\/?p=112\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;SQL Error Handling&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[55],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}