Blog Layout

SQL SERVER TRY CATCH BLOCK SAMPLES

E.DIN Editor • Apr 22, 2020
SQL SERVER TRY CATCH BLOCK SAMPLES

/************************************************************************/
IF @Err = 1
BEGIN
 exec msdb.dbo.sp_send_dbmail
 @profile_name='ReportingMsgs'
 ,@recipients='Alerts@mydomain.com;DataOperations@mydomain.com'
 ,@subject='Error occurred in the SuperAwesome SQL job run'
 ,@body_format='HTML'
 ,@body=@ErrMsg
END
/************************************************************************/
BEGIN CATCH
          SELECT @ErrorMessage = ERROR_MESSAGE();
          RAISERROR (@ErrorMessage, 16, 1);
          RETURN
END CATCH
/************************************************************************/
BEGIN CATCH
  SELECT @ErrorMessage = ERROR_MESSAGE(), @ErrorSeverity = ERROR_SEVERITY(),@ErrorState = ERROR_STATE();
  RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState );
  RETURN
END CATCH
/************************************************************************/
BEGIN CATCH
  RAISERROR('Unable to add new client', 16, 1)
        RETURN
END CATCH
/************************************************************************/
begin catch
  declare @msg varchar(max) = 'A DM Failed. Error Message: ' + error_message()
  exec SUPPORT.dbo.ReportErrorEmail
   'DM Failure'
   , @msg
   ,'Alerts@mydomain.com;DataOperations@mydomain.com'
end catch
/************************************************************************/

BEGIN CATCH
  DECLARE @error2 varchar(max)
  DECLARE @msg2 varchar(max)

  SELECT @error2 = ERROR_MESSAGE();

  SET @msg2 = 'Exception occurred in proc, '+ @@servername + '.'+db_name() +'.dbo.BillingForEntity, when processing BillingEntity ' + @BillingEntity
     + '. Exception detail: ' + @error2
  EXEC msdb.dbo.sp_send_dbmail
  @profile_name='ReportingMsgs',
  @recipients='Alerts@mydomain.com;DataOperations@mydomain.com',
  @subject='Billing Invoice Create Exception',
  @body=@msg2,
  @importance='High'
            
        RETURN;
END CATCH
/************************************************************************/
BEGIN CATCH
     if(ERROR_NUMBER()=242) or (ERROR_Message() like '%date%')
  begin

   select 'Line '+@noRecordsChar +':Invalid Invoice Date detected.This is not editable cell.';
  end
  else
  begin
        Select 'Error Occured WHile Validating The Payment Records.';
  end

END CATCH
/************************************************************************/
By E.DIN Editor 17 Nov, 2021
Title: Base64 Encoding With Simple Salt (and decoding)
By E.DIN Editor 17 Nov, 2021
XPCMD_SHELL Move & Load Data from Directory
By E.DIN Editor 17 Nov, 2021
By E.DIN Editor 17 Nov, 2021
By E.DIN Editor 01 Oct, 2020
 ***** My Valid Data Monitor **** XML OUT *** HTML EMAIL DELIVERY  *****IF OBJECT_ID('tempdb..#tCustomerID') IS NOT NULL DROP TABLE #tCustomerID    select distinct         mm.CustomerID  Into #tCustomerID    from myDatabase..myTable (nolock) mm     left join myDatabase.dbo.myOtherTable (nolock) mo  on mm.refID = mo.id     left join myDatabase.dbo.myOtherTableDetail (nolock) mod  on mo.id = mod.myOtherTableid         and mm.SearchValue = mod.SearchValue     where mm.refID is not null     and mm.quantity is not null      and mod.SearchValue is nullIF Exists (Select 1 From #tCustomerID)BEGIN DECLARE @xml NVARCHAR(MAX)DECLARE @body NVARCHAR(MAX)SET @xml = CAST(( SELECT [CustomerID] AS 'td' FROM #tCustomerID ORDER BY CustomerID FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))SET @body ='

My Valid Data Monitor

'    SET @body = @body + @xml +'
CustomerID
'--SELECT @body  exec msdb.dbo.sp_send_dbmail  @profile_name  = 'dbmailProfile' ,@recipients    = 'usergroup@mydomain.com' ,@subject       = 'My Valid Data Monitor' ,@body          = @body ,@body_format   = 'HTML'END
By websitebuilder 20 Sep, 2020
The new season is a great reason to make and keep resolutions. Whether it’s eating right or cleaning out the garage, here are some tips for making and keeping resolutions.
By websitebuilder 20 Sep, 2020
There are so many good reasons to communicate with site visitors. Tell them about sales and new products or update them with tips and information.
By websitebuilder 20 Sep, 2020
Write about something you know. If you don’t know much about a specific topic that will interest your readers, invite an expert to write about it.
By tekp 15 Jul, 2020
Turn Up! Not every microphone has the same base volume for transmitting your voice through your computer to whoever or whatever is on the other end of the exchange. Some microphones have a higher volume as compared to others, and some microphones have a volume that is so low that the person on the other […] The post How to Turn Up Mic Volume in Windows 10 – Appuals.com appeared first on TekPreacher.
By E.DIN Editor 09 Jun, 2020
'+@UNCPathAndLogTable+'
More Posts
Share by: