title: Using temp tables in IF .. ELSE statements
body: <p>Why does SQL Server insist that the temp table already exists!  one or the other will happen!! , so it will never be the case.</p>
  
  <pre><code>declare @checkvar  varchar(10)
  declare @tbl TABLE( colx varchar(10) )
  set @checkvar ='a'
  
  INSERT  INTO @tbl (colx) VALUES('a')
  INSERT  INTO @tbl (colx) VALUES('b')
  INSERT  INTO @tbl (colx) VALUES('c')
  INSERT  INTO @tbl (colx) VALUES('d')
  
  IF @checkvar  is null  select colx INTO #temp1 FROM @tbl
  ELSE select colx INTO #temp1 FROM @tbl WHERE colx =@checkvar
  </code></pre>
  
  <p>error is :There is already an object named '#temp1' in the database.</p>
  
  <p>Is there an elegant way around this?
  if @checkvar is null, i want the whole table
  otherwise, give me just the values where @checkvar = something</p>
  
  <p>EDIT: the column is a varchar, not an int. </p>