title: Global variables in SQL
body: <p>Let's say I wanted to create a sql script and do something like this:</p>
  
  <pre><code>DECLARE @SomeVariable int
  SET @SomeVariable = 'VALUE'
    FROM someTable
  --do stuff with @SomeVariable
  GO
  
  CREATE PROCEDURE myProcedure
  (
    @MyParameter
  )
  AS
  SET NOCOUNT ON
  
  --Do something
  --Do something using @SomeVariable
  SET NOCOUNT OFF
  RETURN 0
  GO
  </code></pre>
  
  <p>I can't, because @SomeVariable dies with the batch he belongs to, and myProcedure requires its own batch.  Obviously I could create a #temp table and stuff any values I needed in there, but then I would have to select from it - adding code that, while trivial, hurts readability and seems silly when all I need is a global variable.  Is there a better way?</p>
  
  <p>To be painfully clear.  I KNOW SQL Server has "global variables" called "tables" - I mentioned in the above paragraph that using a #table is a possible solution, as is using an actual permanent table.  What I'm looking for here is probably more of a global constant that I can use anywhere within a given script, not a global variable - so we can all stop wetting our pants about the evils of global variables.</p>