title: Getting XQuery [modify()]: Top-level attribute nodes are not supported error while modifying xml
body: <pre><code>DECLARE @mycur CURSOR
  DECLARE @id int
  DECLARE @ParentNodeName varchar(max)
  DECLARE @NodeName varchar(max)
  DECLARE @NodeText varchar(max)
  
  SET @mycur = CURSOR
  FOR
  SELECT * FROM @temp
  
  OPEN @mycur
  
  FETCH NEXT FROM @mycur INTO @id,@ParentNodeName,@NodeName,@NodeText
  
  WHILE @@FETCH_STATUS = 0
  BEGIN
  PRINT @id -- sample statements
  PRINT  @ParentNodeName
  PRINT  @NodeName
          SET @x.modify
          ('
              insert attribute status {sql:variable("@status")}
              as first
              into (/@ParentNodeName/@NodeName/child::*[position()=sql:variable("@status")])[1] 
          ')
  FETCH NEXT FROM @mycur INTO  @id,@ParentNodeName,@NodeName,@NodeText
  END
  
  DEALLOCATE @mycur
  </code></pre>
  
  <p>Any idea why I am getting this error as query works fine if I manually insert path</p>