Query #xml variable to get a rowset - sql

I have an xml data that looks like below.
DECLARE #XmlContent XML
SET #XmlContent =
'<Entities>
<Entity type = "5">
<item id ="1"/>
<item id ="2"/>
<item id ="2"/>
</Entity>
<Entity type = "6">
<item id ="3"/>
<item id ="4"/>
<item id ="5"/>
</Entity>
</Entities>'
I want to select data from this and insert into a table in the following format -
------------
Type Id
------------
5 1
5 2
5 2
6 3
6 4
6 5
Can some one help me to write query for this in sql server?

select
ent.value('#type', 'int') as Type,
row.value('#id', 'int') as ID
from
#XmlContent.nodes('/Entities/Entity') foo(ent)
cross apply ent.nodes('item') bar(row)

Related

Stuck in T-SQL : XML to temp table

I have a little challenge where I am currently stuck in.
This is my query:
DECLARE #xml XML
SET #xml =
'<beginmsg>
<refmsg>A1234567</refmsg>
<shipments>
<shipment>
<load>
<address>
<name>Loadname</name>
<street>Street</street>
<zipcode>ABCDE</zipcode>
<city>Munchen</city>
<countrycode>DE</countrycode>
</address>
<dateTime>
<date>2021-02-02</date>
<timeFrom>08:00:00</timeFrom>
</dateTime>
</load>
<unload>
<address>
<name>unloadname</name>
<street>Street unload</street>
<zipcode>1111</zipcode>
<city>Dresden</city>
<countrycode>DE</countrycode>
</address>
</unload>
<goods>
<good>
<description>Cookies</description>
<quantity>1</quantity>
</good>
<good>
<description>Cookies</description>
<quantity>3</quantity>
</good>
<good>
<description>Some food</description>
<quantity>2</quantity>
</good>
</goods>
<barcodes>
<barcode>7829348742910092309325</barcode>
<barcode>7829348742112344114414</barcode>
<barcode>0984746166149566188446</barcode>
</barcodes>
<references>
<reference>GBP-4362</reference>
</references>
</shipment>
<shipment>
<load>
<address>
<name>shipment 2 load</name>
<street>Street load2</street>
<zipcode>1234 RR</zipcode>
<city>Koln</city>
<countrycode>DE</countrycode>
</address>
<dateTime>
<date>2021-03-03</date>
<timeFrom>10:00:00</timeFrom>
</dateTime>
</load>
<unload>
<address>
<name>Shipment 2 unload</name>
<street>Street 2 unl</street>
<zipcode>1000 AA</zipcode>
<city>Amsterdam</city>
<countrycode>NL</countrycode>
</address>
</unload>
<goods>
<good>
<description>Televisions</description>
<quantity>2</quantity>
</good>
</goods>
<barcodes>
<barcode>0984746166149566188446</barcode>
</barcodes>
<references>
<reference>HBR-7211CX</reference>
</references>
</shipment>
</shipments>
</beginmsg>'
IF OBJECT_ID('tempdb..#Goods') IS NOT NULL DROP TABLE #Goods;
CREATE TABLE #Goods
(
row INT,
shipmentreference VARCHAR(100),
description VARCHAR(MAX),
quantity INT,
barcode VARCHAR(MAX)
);
INSERT INTO #Goods
SELECT
"row" = ROW_NUMBER() OVER (PARTITION BY shipment.shipment ORDER BY shipment.shipment),
"shipmentreference" = CAST(shipment.shipment.value('(references/reference)[1]', 'VARCHAR(100)') AS VARCHAR(MAX)) + '-' + CAST(ROW_NUMBER() OVER (PARTITION BY shipment.shipment ORDER BY shipment.shipment) AS VARCHAR(MAX)),
"description" = goods.goods.value('(description)[1]', 'VARCHAR(10)'),
"quantity" = goods.goods.value('(quantity)[1]', 'VARCHAR(10)'),
"barcode" = barcode.barcode.value('(barcode)[1]', 'VARCHAR(100)')
FROM
#Xml.nodes('beginmsg/shipments/shipment') shipment(shipment)
CROSS APPLY
shipment.shipment.nodes('goods/good') goods(goods)
CROSS APPLY
shipment.shipment.nodes('barcodes') barcode(barcode)
SELECT * FROM #Goods
The XML contains 2 shipments.
In the 1st shipment there are 3 goods segment with each 1 'good' segment. In the 2nd shipment there is one good segment.
I would like to merge each good, with their information and with the 1st barcode for the 1st good, 2nd barcode for the 2nd good etc. etc.
So I was thinking to create a TEMP table #goods and a temp table to store the barcodes in. In both temp tables I would like to add the "reference" so I can join both tables on that unique field.
But at this moment I am stuck. In my final query (as shown above) the output is as follow:
row
reference
description
quantity
barcode
1
GBP-4362-1
Cookies
1
7829348742910092309325
2
GBP-4362-1
Cookies
3
7829348742910092309325
3
GBP-4362-1
Some food
2
7829348742910092309325
1
HBR-7211CX-1
Television
2
0984746166149566188446
So the barcode is each first (what is actually what I am querying :))
How can I iterate through these barcodes nodes and how can I manage to merge it into the row?

Querying XML data in SQL Server 2012

I have a database that has a table Parameters_XML with columns.
id, application, parameter_nr flag, value
The parameter_nr is for example 1 and the value for that parameter is the following:
<Root>
<Row>
<Item id="341" flags="1">
<Str>2</Str>
</Item>
<Item id="342" flags="1">
<Str>10</Str>
</Item>
<Item id="2196" flags="1">
<Str>7REPJ1</Str>
</Item>
</Row>
</Root>
I need to retrieve the values for all the applications where the item is 341, 342 and 2196.
Eg: for the application 1 the value for the item 341 is 2 and so on.
I have written the following query:
SELECT cast (value as XML).value('data(/Root/Row/Item[#id="431"],')
FROM Parameters_Xml x
WHERE parameter_nr = 1
I get the following error:
Msg 174, Level 15, State 1, Line 1
The value function requires 2 argument(s).
Why my query is not valid?
Try someting like this:
SELECT
CAST(x.Value AS XML).value('(/Root/Row/Item[#id="341"]/Str)[1]', 'nvarchar(100)')
FROM dbo.Parameters_Xml x
WHERE parameter_nr = 1
You're telling SQL Server to go find the <Item> node (under <Root> / <Row>) with and id=341 (that what I'm assuming - your value in the question doesn't even exist) and then get the first <Str> node under <Item> and return that value
Also: why do you need CAST(x.Value as XML) - if that column contains only XML - why isn't it defined with datatype XML to begin with? If you have this, you don't need any CAST ...
DECLARE #str XML;
SET #str = '<Root>
<Row>
<Item id="341" flags="1">
<Str>2</Str>
</Item>
<Item id="342" flags="1">
<Str>10</Str>
</Item>
<Item id="2196" flags="1">
<Str>7REPJ1</Str>
</Item>
</Row>
</Root>'
-- if you want specific values then
SELECT
xmlData.Col.value('#id','varchar(max)') Item
,xmlData.Col.value('(Str/text())[1]','varchar(max)') Value
FROM #str.nodes('//Root/Row/Item') xmlData(Col)
where xmlData.Col.value('#id','varchar(max)') = 342
--if you want all values then
SELECT
xmlData.Col.value('#id','varchar(max)') Item
,xmlData.Col.value('(Str/text())[1]','varchar(max)') Value
FROM #str.nodes('//Root/Row/Item') xmlData(Col)
--where xmlData.Col.value('#id','varchar(max)') = 342
Edit After CommentIf i query my db: select * from parameters_xml where parameter_nr = 1 i will receive over 10000 rows, each row is like the following: Id app param value 1 1 1 11 I need for all the 10000 apps to retrieve the item id and the value from the XML value - like you did for my eg.
-- declare temp table
declare #temp table
(val xml)
insert into #temp values ('<Root>
<Row>
<Item id="341" flags="1">
<Str>2</Str>
</Item>
<Item id="342" flags="1">
<Str>10</Str>
</Item>
<Item id="2196" flags="1">
<Str>7REPJ1</Str>
</Item>
</Row>
</Root>')
insert into #temp values ('<Root>
<Row>
<Item id="3411" flags="1">
<Str>21</Str>
</Item>
<Item id="3421" flags="1">
<Str>101</Str>
</Item>
<Item id="21961" flags="1">
<Str>7REPJ11</Str>
</Item>
</Row>
</Root>')
-- QUERY
SELECT
xmlData.Col.value('#id','varchar(max)') Item
,xmlData.Col.value('(Str/text())[1]','varchar(max)') Value
FROM #temp AS T
outer apply T.val.nodes('/Root/Row/Item') as xmlData(Col)
Try this:
select cast (value as
XML).value('data(/Root/Row/Item[#id="431"]','nvarchar(max)')
data type is 2nd parameter.

Processing XML Hierarchy with MS SQL

How would I specify the following XML Hierarchy into readable columns in Microsoft SQL?
<transaction id=1>
<item id=1>
<price>1</price>
</item>
<item id=2>
<price>1</price>
</item>
</transaction>
<transaction>
<item id=1>
<price>1</price>
</item>
</transaction>
for instance
select
x.i.value('(????)','Varchar(max)') [TransId]
x.i.value('(????)','Varchar(max)') [ItemId]
x.i.value('(????)','Varchar(max)') [PriceId]
from #xml.nodes('/transaction') x(i)
Thanks in advance.
Attribute values must always appear in quotation marks in XML.
Not sure about the desired output. An example would be:
declare #xml xml
Select #xml=
'<transaction id="1">
<item id="1">
<price>1</price>
</item>
<item id="2">
<price>2</price>
</item>
</transaction>
<transaction>
<item id="1">
<price>3</price>
</item>
</transaction>'
SELECT
y.value('../#id','int') as TransactionID,
y.value('#id','int') as ItemID,
y.value('(./price/text())[1]', 'Varchar(max)') as Price
FROM #xml.nodes('/transaction/item') as x(y)
order by TransactionID,ItemID
with the output:
NULL 1 3
1 1 1
1 2 2
Actually usually it's faster to shred XML from parent to child using apply, like this:
select
t.c.value('#id','int') as TransId,
i.c.value('#id','int') as ItemId,
i.c.value('(price/text())[1]', 'int') as PriceId
from #xml.nodes('transaction') as t(c)
outer apply t.c.nodes('item') as i(c)
order by TransId, ItemID
sql fiddle demo

select rownumber in xquery and convert it to sql rows

i have a table which i save user's search result rows in xml format as follows :
<row id="5083" />
<row id="5085" />
<row id="5087" />
<row id="5090" />
<row id="5094" />
... (about 500,000 rows)
where each row element contains id of the result record which will be shown in result page.
now i need to select ids of one specific page for example page 2 ( 10th element to 20th element )
the first problem is how can i get that result in xquery ?
i tried to use position() function but it didn't work ...
select #results.query('for $x in (row)
where $x/position() > 10
return ($x)')
and the second prblem is how can get my result ids as sql rows instead of xmlnodelist ?
You can use the nodes() method to shred your XML and use the value() method to extract the value for attribute id.
SQL Fiddle
Query 1:
declare #results xml = '
<row id="5083" />
<row id="5085" />
<row id="5087" />
<row id="5090" />
<row id="5094" />'
select T.N.value('#id', 'int') as id
from #results.nodes('row[position() >= 2 and position() < 4]') as T(N)
Results:
| ID |
--------
| 5085 |
| 5087 |
In
for $x in (row) where $x/position() > 10 return ($x)
$x iterates over all the row children after the 10th but for each returns a sequence of length 1 with just one row element so $x/position() is always 1.
You could use
row[position() >=10 and position() < 20]
which will return elements in positions 10 to 19. If you want just the id's rather than the element nodes then
row[position() >=10 and position() < 20]/string(#id)

Pull out Attributes from TSQL XML

I have the following XML:
<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>
I have this stored in an XML DataType in SQL server and would like to pull out the fields and help text seperately. I.e a results set similar to:
| field1 | helptext |
| field2 | helptext |
| field3 | helptext |
Does anybody know how to achieve this?
Kindest Regards,
Adam
DECLARE #xml AS XML = '<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>'
select c.value('#field', 'varchar(max)') field, c.value('#help', 'varchar(max)') help
from #xml.nodes('/help/item') T(c);
To achieve the same when xml data is in a table field use:
SELECT c.value('#field', 'varchar(max)') field, c.value('#help', 'varchar(max)') help
FROM MyTable A CROSS APPLY A.MyXmlField.nodes('/help/item') T(c)
Where MyTable is the table with field MyXmlField containing xml data.
See if following helps:
DECLARE #processedXmlDoc int
DECLARE #inputDoc varchar(1000)
EXEC sp_xml_preparedocument #processedXmlDoc OUTPUT, #inputDoc
SELECT *
FROM OPENXML (#processedXmlDoc , '/help/item',1)
WITH (field varchar(20) 'field',
help varchar(20) 'helpText')
EXECUTE sp_xml_removedocument #idoc
Hope that helps.
Try something like this:
DECLARE #input XML = '<help>
<item field="field1" help="helptext" />
<item field="field2" help="helptext" />
<item field="field3" help="helptext" />
</help>'
SELECT
Field = Item.value('#field', 'varchar(25)'),
HELP = Item.value('#help', 'varchar(50)')
FROM
#input.nodes('/help/item') AS XTbl(Item)