title: Ad Hoc query assigns results to local variables
body: <p>Hi is it possible for me to do this somehow? When i run the statement i get an exception, @Price_Plan is not delared, so obviously the adhoc query does not have scope to access @Price_Plan. Is there a workaround, or a better way to query a table whose name changes per execution of this query.</p>
  
  <pre><code>DECLARE @Price_Plan varchar(3), @MNP_Network varchar(3), @GSM_Code varchar(3), @GEO_Dist varchar(6),
          @Call_ProdNo varchar(7), @Call_Time datetime, @CallId int, @dtl_call_dur int,
          @Volume varchar(10), @Call_Cost int
  
  --Assume CallId has a value
  
  SET @Sql =
      'SELECT
          @Price_Plan = Price_Plan, @MNP_Network = MNP_Network, @GSM_Code = GSM_Code, @GEO_Dist = GEO_Dist,
          @Call_ProdNo = Call_ProdNo, @Call_Time = Call_Time, @dtl_call_dur = dtl_call_dur,
          @Volume = Volume
       FROM ' + @TableName + ' 
       WHERE CallId = ' + CONVERT(varchar(10),@CallId) + ''
      PRINT  @SQL
      EXEC (@Sql)
  </code></pre>