Generating XML file from SQL query












3














I'm trying to generate a XML file from SQL query and I have a table that contains following columns:



ItemNumber, Price, DateFrom, DateTo


The code I use to generate the XML is:



SELECT
ItemNumber AS '@ItemNumber',
Price AS '@Price',
DateFrom AS 'DateFrom',
DateTo AS 'DateTo'
FROM
#tempXML
FOR XML PATH('Item')


what I expect to get is something like this:



<Item id="111">
<ItemNumber>111</ItemNumber>
<Price value="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Price>
<Price value="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>
<Item>
<ItemNumber>120</ItemNumber>
<Price value="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>


but instead I get something more like this:



<Item 
ItemNumber="111"
Price="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Item>
<Item
ItemNumber="111"
Price="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>
<Item
ItemNumber="120"
Price="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>


Any kind of help will be greatly appreciated



Sample data from the table I use



CREATE TABLE #tempXML
(
ItemNumber INT,
Price INT,
DateFrom DATE,
DateTo DATE
)

INSERT INTO #tempXML
VALUES
(111, 3000, '2018-01-02', '2018-01-30'),
(111, 2500, '2018-01-31', '2018-11-22'),
(120, 4000, '2018-01-12', '2018-11-22')









share|improve this question
























  • Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
    – Larnu
    Nov 22 at 16:56










  • ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
    – mjohansen
    Nov 22 at 17:13










  • That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
    – Larnu
    Nov 22 at 17:14










  • Added to the question @Larnu
    – mjohansen
    Nov 22 at 17:18












  • great, thanks. this gives us something much better to work with. :)
    – Larnu
    Nov 22 at 18:24
















3














I'm trying to generate a XML file from SQL query and I have a table that contains following columns:



ItemNumber, Price, DateFrom, DateTo


The code I use to generate the XML is:



SELECT
ItemNumber AS '@ItemNumber',
Price AS '@Price',
DateFrom AS 'DateFrom',
DateTo AS 'DateTo'
FROM
#tempXML
FOR XML PATH('Item')


what I expect to get is something like this:



<Item id="111">
<ItemNumber>111</ItemNumber>
<Price value="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Price>
<Price value="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>
<Item>
<ItemNumber>120</ItemNumber>
<Price value="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>


but instead I get something more like this:



<Item 
ItemNumber="111"
Price="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Item>
<Item
ItemNumber="111"
Price="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>
<Item
ItemNumber="120"
Price="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>


Any kind of help will be greatly appreciated



Sample data from the table I use



CREATE TABLE #tempXML
(
ItemNumber INT,
Price INT,
DateFrom DATE,
DateTo DATE
)

INSERT INTO #tempXML
VALUES
(111, 3000, '2018-01-02', '2018-01-30'),
(111, 2500, '2018-01-31', '2018-11-22'),
(120, 4000, '2018-01-12', '2018-11-22')









share|improve this question
























  • Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
    – Larnu
    Nov 22 at 16:56










  • ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
    – mjohansen
    Nov 22 at 17:13










  • That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
    – Larnu
    Nov 22 at 17:14










  • Added to the question @Larnu
    – mjohansen
    Nov 22 at 17:18












  • great, thanks. this gives us something much better to work with. :)
    – Larnu
    Nov 22 at 18:24














3












3








3







I'm trying to generate a XML file from SQL query and I have a table that contains following columns:



ItemNumber, Price, DateFrom, DateTo


The code I use to generate the XML is:



SELECT
ItemNumber AS '@ItemNumber',
Price AS '@Price',
DateFrom AS 'DateFrom',
DateTo AS 'DateTo'
FROM
#tempXML
FOR XML PATH('Item')


what I expect to get is something like this:



<Item id="111">
<ItemNumber>111</ItemNumber>
<Price value="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Price>
<Price value="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>
<Item>
<ItemNumber>120</ItemNumber>
<Price value="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>


but instead I get something more like this:



<Item 
ItemNumber="111"
Price="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Item>
<Item
ItemNumber="111"
Price="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>
<Item
ItemNumber="120"
Price="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>


Any kind of help will be greatly appreciated



Sample data from the table I use



CREATE TABLE #tempXML
(
ItemNumber INT,
Price INT,
DateFrom DATE,
DateTo DATE
)

INSERT INTO #tempXML
VALUES
(111, 3000, '2018-01-02', '2018-01-30'),
(111, 2500, '2018-01-31', '2018-11-22'),
(120, 4000, '2018-01-12', '2018-11-22')









share|improve this question















I'm trying to generate a XML file from SQL query and I have a table that contains following columns:



ItemNumber, Price, DateFrom, DateTo


The code I use to generate the XML is:



SELECT
ItemNumber AS '@ItemNumber',
Price AS '@Price',
DateFrom AS 'DateFrom',
DateTo AS 'DateTo'
FROM
#tempXML
FOR XML PATH('Item')


what I expect to get is something like this:



<Item id="111">
<ItemNumber>111</ItemNumber>
<Price value="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Price>
<Price value="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>
<Item>
<ItemNumber>120</ItemNumber>
<Price value="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>


but instead I get something more like this:



<Item 
ItemNumber="111"
Price="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Item>
<Item
ItemNumber="111"
Price="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>
<Item
ItemNumber="120"
Price="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Item>


Any kind of help will be greatly appreciated



Sample data from the table I use



CREATE TABLE #tempXML
(
ItemNumber INT,
Price INT,
DateFrom DATE,
DateTo DATE
)

INSERT INTO #tempXML
VALUES
(111, 3000, '2018-01-02', '2018-01-30'),
(111, 2500, '2018-01-31', '2018-11-22'),
(120, 4000, '2018-01-12', '2018-11-22')






sql sql-server tsql sql-server-2008






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 17:18

























asked Nov 22 at 16:51









mjohansen

213




213












  • Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
    – Larnu
    Nov 22 at 16:56










  • ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
    – mjohansen
    Nov 22 at 17:13










  • That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
    – Larnu
    Nov 22 at 17:14










  • Added to the question @Larnu
    – mjohansen
    Nov 22 at 17:18












  • great, thanks. this gives us something much better to work with. :)
    – Larnu
    Nov 22 at 18:24


















  • Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
    – Larnu
    Nov 22 at 16:56










  • ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
    – mjohansen
    Nov 22 at 17:13










  • That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
    – Larnu
    Nov 22 at 17:14










  • Added to the question @Larnu
    – mjohansen
    Nov 22 at 17:18












  • great, thanks. this gives us something much better to work with. :)
    – Larnu
    Nov 22 at 18:24
















Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
– Larnu
Nov 22 at 16:56




Where is ItemNumber coming from? That's not in your table's definition that you provide. Could you also provide sample data that isn't xml (i.e. is in the format your data is actually currently in). thanks.
– Larnu
Nov 22 at 16:56












ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
– mjohansen
Nov 22 at 17:13




ItemNumber is a column in the #tempXML table, updated the question. Also added the data sample screen @Larnu
– mjohansen
Nov 22 at 17:13












That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
– Larnu
Nov 22 at 17:14




That's a weblink to an image, please supply the sample data as text, or (even better), as DDL and DML statements.
– Larnu
Nov 22 at 17:14












Added to the question @Larnu
– mjohansen
Nov 22 at 17:18






Added to the question @Larnu
– mjohansen
Nov 22 at 17:18














great, thanks. this gives us something much better to work with. :)
– Larnu
Nov 22 at 18:24




great, thanks. this gives us something much better to work with. :)
– Larnu
Nov 22 at 18:24












3 Answers
3






active

oldest

votes


















1














You can specify how elements should be nested like this:



SELECT
ItemNumber AS '@id',
ItemNumber AS 'ItemNumber',
Price AS 'Price/@value',
DateFrom AS 'Price/DateFrom',
DateTo AS 'Price/DateTo'
FROM
#tempXML
FOR XML PATH('Item');


see https://docs.microsoft.com/en-us/sql/relational-databases/xml/columns-with-a-name?view=sql-server-2017






share|improve this answer























  • This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
    – Larnu
    Nov 22 at 18:39





















1














This appears to be what you are after, however, it is a little messy:



SELECT T.ItemNumber AS [@ID],
(SELECT T.ItemNumber,
(SELECT sq.Price AS [@value],
(SELECT sq.DateFrom,
sq.DateTo
FOR XML PATH(''),TYPE)
FROM #tempXML sq
WHERE sq.ItemNumber = T.ItemNumber
FOR XML PATH('Price'),TYPE)
FOR XML PATH(''),TYPE)
FROM #tempXML T
GROUP BY ItemNumber
FOR XML PATH ('Item');


This results in:



<Item ID="111">
<ItemNumber>111</ItemNumber>
<Price value="3000">
<DateFrom>2018-01-02</DateFrom>
<DateTo>2018-01-30</DateTo>
</Price>
<Price value="2500">
<DateFrom>2018-01-31</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>
<Item ID="120">
<ItemNumber>120</ItemNumber>
<Price value="4000">
<DateFrom>2018-01-12</DateFrom>
<DateTo>2018-11-22</DateTo>
</Price>
</Item>





share|improve this answer





























    0














    And one more suggestion, a bit simpler...



    SELECT t1.ItemNumber AS [@id]
    ,t1.ItemNumber
    ,(
    SELECT t2.Price AS [@value]
    ,t2.DateFrom
    ,t2.DateTo
    FROM #tempXML t2
    WHERE t1.ItemNumber=t2.ItemNumber
    FOR XML PATH('Price'),TYPE
    )

    FROM #tempXML t1
    GROUP BY t1.ItemNumber
    FOR XML PATH('Item');


    Some explanantion:



    The GROUP BY will reduce the outer SELECT to one-row-per-ItemNumber, while the sub-select will act as a correlated sub-query and fetch all price-details for the given Item.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435386%2fgenerating-xml-file-from-sql-query%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      You can specify how elements should be nested like this:



      SELECT
      ItemNumber AS '@id',
      ItemNumber AS 'ItemNumber',
      Price AS 'Price/@value',
      DateFrom AS 'Price/DateFrom',
      DateTo AS 'Price/DateTo'
      FROM
      #tempXML
      FOR XML PATH('Item');


      see https://docs.microsoft.com/en-us/sql/relational-databases/xml/columns-with-a-name?view=sql-server-2017






      share|improve this answer























      • This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
        – Larnu
        Nov 22 at 18:39


















      1














      You can specify how elements should be nested like this:



      SELECT
      ItemNumber AS '@id',
      ItemNumber AS 'ItemNumber',
      Price AS 'Price/@value',
      DateFrom AS 'Price/DateFrom',
      DateTo AS 'Price/DateTo'
      FROM
      #tempXML
      FOR XML PATH('Item');


      see https://docs.microsoft.com/en-us/sql/relational-databases/xml/columns-with-a-name?view=sql-server-2017






      share|improve this answer























      • This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
        – Larnu
        Nov 22 at 18:39
















      1












      1








      1






      You can specify how elements should be nested like this:



      SELECT
      ItemNumber AS '@id',
      ItemNumber AS 'ItemNumber',
      Price AS 'Price/@value',
      DateFrom AS 'Price/DateFrom',
      DateTo AS 'Price/DateTo'
      FROM
      #tempXML
      FOR XML PATH('Item');


      see https://docs.microsoft.com/en-us/sql/relational-databases/xml/columns-with-a-name?view=sql-server-2017






      share|improve this answer














      You can specify how elements should be nested like this:



      SELECT
      ItemNumber AS '@id',
      ItemNumber AS 'ItemNumber',
      Price AS 'Price/@value',
      DateFrom AS 'Price/DateFrom',
      DateTo AS 'Price/DateTo'
      FROM
      #tempXML
      FOR XML PATH('Item');


      see https://docs.microsoft.com/en-us/sql/relational-databases/xml/columns-with-a-name?view=sql-server-2017







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 22 at 17:41

























      answered Nov 22 at 17:14









      cpt_dood

      2116




      2116












      • This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
        – Larnu
        Nov 22 at 18:39




















      • This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
        – Larnu
        Nov 22 at 18:39


















      This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
      – Larnu
      Nov 22 at 18:39






      This is close, but isn't quite what the OP is after. If you look, you'll notice the OP only has one node for Item id="111", which contains multiple Price nodes. This will produce 2 Item id="111" nodes, each with it own nodes for Price.
      – Larnu
      Nov 22 at 18:39















      1














      This appears to be what you are after, however, it is a little messy:



      SELECT T.ItemNumber AS [@ID],
      (SELECT T.ItemNumber,
      (SELECT sq.Price AS [@value],
      (SELECT sq.DateFrom,
      sq.DateTo
      FOR XML PATH(''),TYPE)
      FROM #tempXML sq
      WHERE sq.ItemNumber = T.ItemNumber
      FOR XML PATH('Price'),TYPE)
      FOR XML PATH(''),TYPE)
      FROM #tempXML T
      GROUP BY ItemNumber
      FOR XML PATH ('Item');


      This results in:



      <Item ID="111">
      <ItemNumber>111</ItemNumber>
      <Price value="3000">
      <DateFrom>2018-01-02</DateFrom>
      <DateTo>2018-01-30</DateTo>
      </Price>
      <Price value="2500">
      <DateFrom>2018-01-31</DateFrom>
      <DateTo>2018-11-22</DateTo>
      </Price>
      </Item>
      <Item ID="120">
      <ItemNumber>120</ItemNumber>
      <Price value="4000">
      <DateFrom>2018-01-12</DateFrom>
      <DateTo>2018-11-22</DateTo>
      </Price>
      </Item>





      share|improve this answer


























        1














        This appears to be what you are after, however, it is a little messy:



        SELECT T.ItemNumber AS [@ID],
        (SELECT T.ItemNumber,
        (SELECT sq.Price AS [@value],
        (SELECT sq.DateFrom,
        sq.DateTo
        FOR XML PATH(''),TYPE)
        FROM #tempXML sq
        WHERE sq.ItemNumber = T.ItemNumber
        FOR XML PATH('Price'),TYPE)
        FOR XML PATH(''),TYPE)
        FROM #tempXML T
        GROUP BY ItemNumber
        FOR XML PATH ('Item');


        This results in:



        <Item ID="111">
        <ItemNumber>111</ItemNumber>
        <Price value="3000">
        <DateFrom>2018-01-02</DateFrom>
        <DateTo>2018-01-30</DateTo>
        </Price>
        <Price value="2500">
        <DateFrom>2018-01-31</DateFrom>
        <DateTo>2018-11-22</DateTo>
        </Price>
        </Item>
        <Item ID="120">
        <ItemNumber>120</ItemNumber>
        <Price value="4000">
        <DateFrom>2018-01-12</DateFrom>
        <DateTo>2018-11-22</DateTo>
        </Price>
        </Item>





        share|improve this answer
























          1












          1








          1






          This appears to be what you are after, however, it is a little messy:



          SELECT T.ItemNumber AS [@ID],
          (SELECT T.ItemNumber,
          (SELECT sq.Price AS [@value],
          (SELECT sq.DateFrom,
          sq.DateTo
          FOR XML PATH(''),TYPE)
          FROM #tempXML sq
          WHERE sq.ItemNumber = T.ItemNumber
          FOR XML PATH('Price'),TYPE)
          FOR XML PATH(''),TYPE)
          FROM #tempXML T
          GROUP BY ItemNumber
          FOR XML PATH ('Item');


          This results in:



          <Item ID="111">
          <ItemNumber>111</ItemNumber>
          <Price value="3000">
          <DateFrom>2018-01-02</DateFrom>
          <DateTo>2018-01-30</DateTo>
          </Price>
          <Price value="2500">
          <DateFrom>2018-01-31</DateFrom>
          <DateTo>2018-11-22</DateTo>
          </Price>
          </Item>
          <Item ID="120">
          <ItemNumber>120</ItemNumber>
          <Price value="4000">
          <DateFrom>2018-01-12</DateFrom>
          <DateTo>2018-11-22</DateTo>
          </Price>
          </Item>





          share|improve this answer












          This appears to be what you are after, however, it is a little messy:



          SELECT T.ItemNumber AS [@ID],
          (SELECT T.ItemNumber,
          (SELECT sq.Price AS [@value],
          (SELECT sq.DateFrom,
          sq.DateTo
          FOR XML PATH(''),TYPE)
          FROM #tempXML sq
          WHERE sq.ItemNumber = T.ItemNumber
          FOR XML PATH('Price'),TYPE)
          FOR XML PATH(''),TYPE)
          FROM #tempXML T
          GROUP BY ItemNumber
          FOR XML PATH ('Item');


          This results in:



          <Item ID="111">
          <ItemNumber>111</ItemNumber>
          <Price value="3000">
          <DateFrom>2018-01-02</DateFrom>
          <DateTo>2018-01-30</DateTo>
          </Price>
          <Price value="2500">
          <DateFrom>2018-01-31</DateFrom>
          <DateTo>2018-11-22</DateTo>
          </Price>
          </Item>
          <Item ID="120">
          <ItemNumber>120</ItemNumber>
          <Price value="4000">
          <DateFrom>2018-01-12</DateFrom>
          <DateTo>2018-11-22</DateTo>
          </Price>
          </Item>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 18:32









          Larnu

          15.2k41530




          15.2k41530























              0














              And one more suggestion, a bit simpler...



              SELECT t1.ItemNumber AS [@id]
              ,t1.ItemNumber
              ,(
              SELECT t2.Price AS [@value]
              ,t2.DateFrom
              ,t2.DateTo
              FROM #tempXML t2
              WHERE t1.ItemNumber=t2.ItemNumber
              FOR XML PATH('Price'),TYPE
              )

              FROM #tempXML t1
              GROUP BY t1.ItemNumber
              FOR XML PATH('Item');


              Some explanantion:



              The GROUP BY will reduce the outer SELECT to one-row-per-ItemNumber, while the sub-select will act as a correlated sub-query and fetch all price-details for the given Item.






              share|improve this answer


























                0














                And one more suggestion, a bit simpler...



                SELECT t1.ItemNumber AS [@id]
                ,t1.ItemNumber
                ,(
                SELECT t2.Price AS [@value]
                ,t2.DateFrom
                ,t2.DateTo
                FROM #tempXML t2
                WHERE t1.ItemNumber=t2.ItemNumber
                FOR XML PATH('Price'),TYPE
                )

                FROM #tempXML t1
                GROUP BY t1.ItemNumber
                FOR XML PATH('Item');


                Some explanantion:



                The GROUP BY will reduce the outer SELECT to one-row-per-ItemNumber, while the sub-select will act as a correlated sub-query and fetch all price-details for the given Item.






                share|improve this answer
























                  0












                  0








                  0






                  And one more suggestion, a bit simpler...



                  SELECT t1.ItemNumber AS [@id]
                  ,t1.ItemNumber
                  ,(
                  SELECT t2.Price AS [@value]
                  ,t2.DateFrom
                  ,t2.DateTo
                  FROM #tempXML t2
                  WHERE t1.ItemNumber=t2.ItemNumber
                  FOR XML PATH('Price'),TYPE
                  )

                  FROM #tempXML t1
                  GROUP BY t1.ItemNumber
                  FOR XML PATH('Item');


                  Some explanantion:



                  The GROUP BY will reduce the outer SELECT to one-row-per-ItemNumber, while the sub-select will act as a correlated sub-query and fetch all price-details for the given Item.






                  share|improve this answer












                  And one more suggestion, a bit simpler...



                  SELECT t1.ItemNumber AS [@id]
                  ,t1.ItemNumber
                  ,(
                  SELECT t2.Price AS [@value]
                  ,t2.DateFrom
                  ,t2.DateTo
                  FROM #tempXML t2
                  WHERE t1.ItemNumber=t2.ItemNumber
                  FOR XML PATH('Price'),TYPE
                  )

                  FROM #tempXML t1
                  GROUP BY t1.ItemNumber
                  FOR XML PATH('Item');


                  Some explanantion:



                  The GROUP BY will reduce the outer SELECT to one-row-per-ItemNumber, while the sub-select will act as a correlated sub-query and fetch all price-details for the given Item.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 at 7:28









                  Shnugo

                  48.5k72566




                  48.5k72566






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435386%2fgenerating-xml-file-from-sql-query%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Sphinx de Gizeh

                      Dijon

                      Équipe cycliste