Dynamically generate node names

  • I need to generate XML results from a query and the node names need to be dynamically generated based on the results of that query. Does anyone know how I can produce xml similar to this?

    <root name>

    <product_somethingdynamicgoeshere>

    <recipes>

    <recipe_somethingdynamichere></recipe_somethingdynamichere>

    <recipe_somethingdynamichere></recipe_somethingdynamichere>

    </recipes>

    </root name>

    Thanks in advance for your help!

  • This sounds suspiciously like a homework assignment. The answer to this question is easily found with a little research. The fact that you didn't find it suggests that you didn't even bother to try.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • Definitely not a homework assignment. I am new to this sort of thing and have managed to

    get the data in the following format using a XML Explicit:

    <PRODUCT Product_name=something>

    ....nested tree levels here

    </PRODUCT>

    and I need to have it appear as <PRODUCT_something> -node name.

    If there is an easy solution or if you know somewhere I can check, please pass it along.

    Thanks!

  • Depending on the complexity of your query, you could try FOR XML AUTO, ELEMENTS or FOR XML PATH('some value') instead of XML EXPLICIT.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • You may also need to use dynamic SQL. It's really difficult to tell without DDL, sample data, and expected results to work with.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply