title: If SQL variable exists
body: <p>I'm writing some code that uses a significant quantity of dynamic SQL and as a result, there are some instances when a third party application may or may not have a SQL variable declared.</p>
  
  <p>Is there a way to test if a variable has been declared.
  Some psuedo code would be:</p>
  
  <pre><code>IF OBJECT_ID(N'@var' 
  ) IS NOT NULL
  BEGIN
      DECLARE @var AS varchar(max)
  END
  </code></pre>
  
  <p>Also, is there a way to list all of the variables currently declared like a <em>local watch</em> window? </p>