title: Insert Into From SELECT query
body: <p>This is my SQL code. </p>
  
  <pre><code>CREATE TABLE [dbo].[Basic]
  (
    ID int,
    GroupCode int,
    program int
  )
  
  DECLARE @tblnum int
  
  SELECT tblnum FROM dbo.Program
  
  SET @Qry = 'SELECT Hits_ID, GroupCode  FROM dbo.' + @tblnum 
  
  INSERT INTO [dbo].[Basic]
  (
  ID,
  GroupCode 
  )
  EXEC sp_executesql @Qry
  </code></pre>
  
  <p>The problem is I need to insert values to the table <code>Basic</code> where <code>ID</code> and <code>GroupCode</code> will come from another table then column <code>program</code> should have the value of the variable <code>@tblnum</code>. And I don't know how to do that exactly. Kindly help. Thank you.</p>