title: SQL Server Hanging after Insert Into Select
body: <p>Thanks for taking a look at this.</p>
  
  <p>The issue has to do with the following sql query. Basically it recursively finds all pages underneath a certain page(master page), and then applies the master page's categories(looked up in the xref_pages_categories) to all of those pages.</p>
  
  <p>Upon running the insert, querying the table for data that should have been inserted completely hangs. </p>
  
  <p>If i run the query without the insert line, the results come back perfectly, like:</p>
  
  <pre><code>3245    490
  3249    490
  3252    490
  </code></pre>
  
  <p>But upon putting the insert in front of it and inserting, querying for pageid=3245 just hangs. Any ideas?</p>
  
  <pre><code>declare @page int;
  
  set @page=3202;
  begin transaction transki;
  
  With FindAllPagesUnderneath(PageID, ParentID) as
  (
      select id as PageID, ParentPageId as ParentID
      from pages where id=@page
      union all
      select id as PageID, ParentPageID as ParentID
      from pages p
      inner join FindAllPagesUnderneath mp on
      p.parentpageid=mp.PageID
  )
  
  insert into xref_pages_categories (PageID, CategoryID)
  
  
      --get records that definitely arent already in xref_pages_categories
  select * from
  (
      select distinct thegood.*
      from 
          (
          --get all pages under @page attached to new categoryid
          select distinct fap2.PageID, fapo.CategoryID
              from FindAllPagesUnderneath fap2
              cross join  (
                  select fap.PageID,xpc.categoryid 
                  from xref_pages_categories xpc
                  inner join FindAllPagesUnderneath fap
                  on fap.PageID=xpc.pageid
                  where fap.pageid!=1 and
                  fap.pageid!=1332
                  and fap.parentid=1332
              ) fapo 
          where fap2.pageid !=@page
          ) thegood
          left outer join xref_pages_categories xpc
          on xpc.pageid = thegood.pageid
          where xpc.pageid is null
  ) final
  </code></pre>