title: Insert query using CTE in SQL Server
body: <p>I want to insert into one table from another table using a CTE. I have tried to put semicolon before <code>with</code>, but it's not working.  </p>
  
  <p>This is my query :</p>
  
  <pre><code>INSERT INTO [autoFIE2].[dbo].[tbl_article_type_parent_child] ([art_typ_parent_index], [art_typ_child_index])
  WITH article_type_list AS
  (
      SELECT 
         art_typ_child_index, art_typ_parent_index
      FROM
         [autoFIE2].[dbo].[tbl_article_type_parent_child]  
      WHERE 
         art_typ_parent_index IS NULL 
      UNION ALL
      SELECT 
         a.art_typ_child_index, a.art_typ_parent_index
      FROM
         [autoFIE2].[dbo].[tbl_article_type_parent_child] A 
      INNER JOIN 
         article_type_list as AL ON a.art_typ_parent_index = al.art_typ_child_index
      WHERE 
         a.art_typ_parent_index IS NOT NULL)
  SELECT * 
  FROM article_type_list;
  </code></pre>
  
  <p>Error while executing this statement :-</p>
  
  <blockquote>
    <p>Msg 156, Level 15, State 1, Line 4<br>
    Incorrect syntax near the keyword 'with'.  </p>
    
    <p>Msg 319, Level 15, State 1, Line 4<br>
    Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking
    context clause, the previous statement must be terminated with a
    semicolon.</p>
  </blockquote>
  
  <p>What should I do to insert this hierarchical data into another table. Any suggestion ?</p>