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 Fiddle: http://sqlfiddle.com/#!18/a0602/1
ItemNumbercoming 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.text, or (even better), as DDL and DML statements.