title: T-SQL: List all Variables in Scope
body: <p>I am trying to print all variables in scope of a SP execution after an exception so that I would have more insights of what went wrong during the failure. How can you do that?</p>
  
  <pre class="lang-sql prettyprint-override"><code>BEGIN TRY
      DECLARE @Var1 AS VARCHAR(MAX) = 'variable 1'
      DECLARE @Var2 AS INT = 5
  END TRY
  BEGIN CATCH
      SELECT  @ErMessage = ERROR_MESSAGE(),
              @ErSeverity = ERROR_SEVERITY(),
              @ErState = ERROR_STATE();
  
      RAISERROR(@ErMessage, @ErSeverity, @ErState);
  
      -- Print all @variable values like "@Var1: variable 1, ..."
  
      RETURN -1
  END CATCH
  </code></pre>