Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3594026
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:39:21+00:00 2026-05-18T19:39:21+00:00

I have inherited a 1000-line stored procedure which produces XML by using FOR XML

  • 0

I have inherited a 1000-line stored procedure which produces XML by using FOR XML EXPLICIT. My problem is that it works most of the time. In some scenarios I get the error:

Parent tag ID 2 is not among the open
tags. FOR XML EXPLICIT requires parent
tags to be opened first. Check the
ordering of the result set.
Number:6833 Severity:16 State:1

I need ideas on how to troubleshoot this. I need to find out where the nesting is failing. It’s probably a case of a parent row not being emitted but a child row is. Worse, the problem only happens on our test system, which is probably missing some of the Production data. The question is how to find this out of thousands of rows?

One wild idea that I’m sure doesn’t exist: SQL Server has an algorithm it is using to determine whether the rows are in the correct order. It would be wonderful (if unlikely) if there were a tool that would look at my resultset (without the FOR XML EXPLICIT) and find out where the problem is, then tell me about it.

In the absence of such a tool, I welcome any suggestions on how to debug this. The XML (when it works) is four levels deep!


Update: Thanks for all the answers so far. It is looking like this is a question of a badly-edited stored procedure. Large sections were commented out with “/* /” comments – which don’t work very well when there are already “/ */” comments in the code… I’ll update again when I’m sure of the answer.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T19:39:22+00:00Added an answer on May 18, 2026 at 7:39 pm

    One possible way is to actually remove the FOR XML EXPLICIT part and look at the resulting resultset generated by your sql statement. It will give an indication of the nesting that is generating the xml and hopefully direct you to the issue. See the below image which is taken from the MSDN documentation at:
    http://msdn.microsoft.com/en-us/library/ms189068.aspx.

    alt text


    EDIT

    It may be worth posting a sample output, but in the example in the image you would get the same error if Order!2!Id for any of the rows with tag=3 was null. This column is effectively the join between parent rows with tag=2 and child rows with tag=3. If your data was like above I think you could effectively find your issue by identifying rows with parent=2 and Order!2!Id is null.

    Alternatively it could be ordering. In which case you could somehow build a query that identifies any rows with Parent = 2 occurring before rows with Tag = 2 in the resultset.


    Edit 2

    CREATE TABLE MyTable(
        Tag int,
        Parent int,
        SomeIdentifier int
    )   
    
    INSERT INTO MyTable VALUES (2, 1, 1) -- this row defined before parent
    INSERT INTO MyTable VALUES (1, null, 1)
    INSERT INTO MyTable VALUES (3, 2, 1)
    INSERT INTO MyTable VALUES (3, 2, 1)
    INSERT INTO MyTable VALUES (1, null, 2)
    INSERT INTO MyTable VALUES (2, 1, 2)
    INSERT INTO MyTable VALUES (3, 2, 2)
    INSERT INTO MyTable VALUES (3, 2, 2)
    INSERT INTO MyTable VALUES (1, null, 3)
    INSERT INTO MyTable VALUES (3, 2, 3) -- this is orphaned
    INSERT INTO MyTable VALUES (3, 2, 3) -- this is orphaned
    
    ;WITH myCte AS(
    SELECT   Tag
            ,Parent
            ,SomeIdentifier
            ,ROW_NUMBER() OVER (PARTITION BY SomeIdentifier ORDER BY(SELECT 0)) AS RowOrder
    FROM    MyTable   
    ) SELECT c1.Tag
            ,c1.Parent
            ,c1.SomeIdentifier
    FROM myCte c1 
    LEFT OUTER JOIN myCte c2 ON c2.SomeIdentifier = c1.SomeIdentifier AND c1.Parent = c2.Tag
    WHERE c1.Parent IS NOT NULL     --ignore root rows for now
    AND   (c1.RowOrder < c2.RowOrder    --out of order rows
            OR    
           c2.Tag IS NULL)      --orphaned rows
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have inherited couple of .Net (C#) application which does not have any tracing
I have inherited a somewhat messy query that I am working on refactoring to
I have inherited an application that logs the results of certain daily commands that
I have inherited a DotNetNuke codebase and have come across areas that have included
I have inherited a reasonable sized ASP.net solution that has no automated tests. The
I have inherited a project, that provides an order form to a customer. The
I have been working on a system which I'm using protobuf-net (version 2.0.0.480) for
I have inherited a website! which was designed to work in IE and only
I have a gridview that has some 20 columns and 1000 rows. The grid
I have inherited a flex project which is sadly not documented. Im looking for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.