This is a standard error handler which is called from CATCH blocks.
Parameters
@procid int -- Callers pass @@procid
@sql nvarchar(MAX) = NULL. If non-NULL, the string is printed. This permit diagnostics with dynamic SQL.