I have an XML
<response>
<message_infos>
<message_info>
<id>397300589</id>
<pdu_id>673399673</pdu_id>
<status>12</status>
<id>397300589</id>
<pdu_id>673399675</pdu_id>
<status>12</status>
</message_info>
<message_info>
<id>397300591</id>
<pdu_id>673399669</pdu_id>
<status>12</status>
<id>397300591</id>
<pdu_id>673399671</pdu_id>
<status>12</status>
</message_info>
</message_infos>
</response>
I need to save it as
397300589 673399673 12
397300589 673399675 12
397300591 673399669 12
397300591 673399671 12
But
SELECT *
FROM OPENXML(@ixml, '/response/message_infos/message_info')
WITH (id VARCHAR(50) 'id', pdu_id VARCHAR(50) 'pdu_id', status INT 'status')
giving me wrong result:
397300589 673399673 12
397300591 673399669 12
What am I doing wrong? I didn't find same examples, can someone help me with it?
What I really love on SO is the variety of solutions offered by different people. Here are 4 answers, each of them following a completely different concept.
I was curious how they perform. This is my result:
As there are all nodes taken, approaches reading top-down through the XMLs are obviously faster than approaches with a lot of back and forth navigation. Might be, that some solutions could be improved...
And it was a surprise for me, that the GROUP BY with MAX and CASE
was a slow one...
This is the code:
CREATE TABLE #tmp (ID INT IDENTITY,XmlContent XML);
GO
INSERT INTO #tmp(XmlContent)
SELECT('<response>
<message_infos>
<message_info>
<id>397300589</id>
<pdu_id>673399673</pdu_id>
<status>12</status>
<id>397300589</id>
<pdu_id>673399675</pdu_id>
<status>12</status>
</message_info>
<message_info>
<id>397300591</id>
<pdu_id>673399669</pdu_id>
<status>12</status>
<id>397300591</id>
<pdu_id>673399671</pdu_id>
<status>12</status>
</message_info>
<message_info>
<id>3973005891</id>
<pdu_id>6733996732</pdu_id>
<status>123</status>
<id>3973005894</id>
<pdu_id>6733996755</pdu_id>
<status>126</status>
<id>3973005897</id>
<pdu_id>6733996738</pdu_id>
<status>129</status>
<id>39730058912</id>
<pdu_id>67339967513</pdu_id>
<status>12</status>
</message_info>
</message_infos>
</response>');
GO 1000
--Mikael Eriksson 1,4
DECLARE @d DATETIME = GETDATE();
WITH Numbers AS (SELECT N FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) as tbl(N))
select T.X.value('(id/text())[sql:column("N.N")][1]', 'nvarchar(max)') as id,
T.X.value('(pdu_id/text())[sql:column("N.N")][1]', 'nvarchar(max)') as pdu_id,
T.X.value('(status/text())[sql:column("N.N")][1]', 'int') as status
from #tmp
CROSS APPLY XmlContent.nodes('/response/message_infos/message_info') as T(X)
cross apply (SELECT TOP(T.X.value('count(id)', 'int')) N FROM Numbers) AS N(N)
SELECT 'Mikael Eriksson: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100));
GO
--Shnugo 4.8 Sekunden
DECLARE @d DATETIME = GETDATE();
WITH AllMessageInfos AS
(
SELECT ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) AS Mi_Index
,mi.query('.') AS TheMI
FROM #tmp
CROSS APPLY XmlContent.nodes('/response/message_infos/message_info') AS A(mi)
)
,AllIDs AS
(
SELECT Mi_Index
,ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS id_Index
,id.value('.','nvarchar(max)') AS id
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/id') AS a(id)
)
,AllPdu_IDs AS
(
SELECT Mi_Index
,ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS pdu_Index
,id.value('.','nvarchar(max)') AS pdu_id
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/pdu_id') AS a(id)
)
,AllStatus AS
(
SELECT Mi_Index
,ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS status_Index
,id.value('.','int') AS status
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/status') AS a(id)
)
SELECT i.Mi_Index AS MessageInfoIndex
,i.id_Index AS SubSetIndex
,i.id
,p.pdu_id
,s.status
FROM AllIDs AS i
INNER JOIN AllPdu_IDs AS p ON i.Mi_Index=p.Mi_Index AND i.id_Index=p.pdu_Index
INNER JOIN AllStatus AS s ON i.Mi_Index=s.Mi_Index AND i.id_Index=s.status_Index
SELECT 'Shnugo: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100));
GO
--Montewizdoh 8.8 Sekunden
DECLARE @d DATETIME = GETDATE();
SELECT
m.value('for $i in . return count(../../*[. << $i])', 'int') as message_info_position,
m.value('for $i in . return count(../*[. << $i]) + 1', 'int') as internal_position,
m.value('fn:local-name(.)', 'SYSNAME') as element_name,
m.value('.', 'nvarchar(4000)') as element_value
FROM #tmp
CROSS APPLY XmlContent.nodes('/response/message_infos/message_info/*') AS A(m)
SELECT 'Montewhizdoh: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100));
GO
--Devart 11.3 Sec
DECLARE @d DATETIME = GETDATE();
SELECT
id = MAX(CASE WHEN name = 'id' THEN val END)
, pdu_id = MAX(CASE WHEN name = 'pdu_id' THEN val END)
, [status] = MAX(CASE WHEN name = 'status' THEN val END)
FROM (
SELECT
name = t.c.value('local-name(.)', 'SYSNAME')
, val = t.c.value('.', 'nvarchar(max)')
, RowNum = ROW_NUMBER() OVER (ORDER BY 1/0)
, RowNumGroup = ROW_NUMBER() OVER (ORDER BY 1/0) % 3
FROM #tmp
CROSS APPLY XmlContent.nodes('/response/message_infos/message_info/*') t(c)
) t
GROUP BY RowNum - CASE WHEN RowNumGroup = 0 THEN 3 ELSE RowNumGroup END
SELECT 'Devart: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100));
GO
DROP TABLE #tmp;
DECLARE @xml XML = N'
<response>
<message_infos>
<message_info>
<id>397300589</id>
<pdu_id>673399673</pdu_id>
<status>12</status>
<id>397300589</id>
<pdu_id>673399675</pdu_id>
<status>12</status>
</message_info>
<message_info>
<id>397300591</id>
<pdu_id>673399669</pdu_id>
<status>12</status>
<id>397300591</id>
<pdu_id>673399671</pdu_id>
<status>12</status>
</message_info>
</message_infos>
</response>'
SELECT
id = MIN(CASE WHEN RowNumGroup = 1 THEN val END)
, pdu_id = MIN(CASE WHEN RowNumGroup = 2 THEN val END)
, [status] = MIN(CASE WHEN RowNumGroup = 0 THEN val END)
FROM (
SELECT
val = t.c.value('(./text())[1]', 'BIGINT')
, RowNum = ROW_NUMBER() OVER (ORDER BY 1/0)
, RowNumGroup = ROW_NUMBER() OVER (ORDER BY 1/0) % 3
FROM @xml.nodes('/response/message_infos/message_info/*') t(c)
) t
GROUP BY RowNum - CASE WHEN RowNumGroup = 0 THEN 3 ELSE RowNumGroup END
UPDATE 30/05/2016
Prepare data:
SET NOCOUNT ON
IF OBJECT_ID('tempdb.dbo.#xml') IS NOT NULL
DROP TABLE #xml
GO
CREATE TABLE #xml (data XML)
GO
INSERT INTO #xml
SELECT TOP(10000) N'
<response>
<message_infos>
<message_info>
<id>397300589</id>
<pdu_id>673399673</pdu_id>
<status>12</status>
<id>397300589</id>
<pdu_id>673399675</pdu_id>
<status>12</status>
</message_info>
<message_info>
<id>397300591</id>
<pdu_id>673399669</pdu_id>
<status>12</status>
<id>397300591</id>
<pdu_id>673399671</pdu_id>
<status>12</status>
</message_info>
</message_infos>
</response>'
FROM [master].dbo.spt_values
Mikael Eriksson
DECLARE @d DATETIME = GETDATE()
SELECT T.X.value('(id/text())[sql:column("N.N")][1]', 'INT') AS id,
T.X.value('(pdu_id/text())[sql:column("N.N")][1]', 'INT') AS pdu_id,
T.X.value('(status/text())[sql:column("N.N")][1]', 'INT') AS [status]
FROM #xml x
CROSS APPLY data.nodes('/response/message_infos/message_info') AS T(X)
CROSS APPLY (
VALUES
(1),(2),(3),(4),(5),
(6),(7),(8),(9),(10)
) AS N(N)
WHERE N.N <= T.X.value('count(id)', 'INT')
SELECT 'Mikael Eriksson: ' + CAST(CAST(GETDATE() - @d AS TIME) AS VARCHAR(100))
GO
Shnugo
DECLARE @d DATETIME = GETDATE()
WITH AllMessageInfos AS (
SELECT ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) AS Mi_Index
, mi.query('.') AS TheMI
FROM #xml x
CROSS APPLY data.nodes('/response/message_infos/message_info') AS A(mi)
)
, AllIDs AS (
SELECT Mi_Index
, ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS id_Index
, id.value('.','NVARCHAR(MAX)') AS id
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/id') AS a(id)
)
, AllPdu_IDs AS (
SELECT Mi_Index
, ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS pdu_Index
, id.value('.','NVARCHAR(MAX)') AS pdu_id
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/pdu_id') AS a(id)
)
, AllStatus AS (
SELECT Mi_Index
, ROW_NUMBER() OVER(PARTITION BY Mi_Index ORDER BY (SELECT NULL)) AS status_Index
, id.value('.', 'INT') AS status
FROM AllMessageInfos
CROSS APPLY TheMI.nodes('message_info/status') AS a(id)
)
SELECT i.Mi_Index AS MessageInfoIndex
, i.id_Index AS SubSetIndex
, i.id
, p.pdu_id
, s.[status]
FROM AllIDs AS i
JOIN AllPdu_IDs AS p ON i.Mi_Index = p.Mi_Index AND i.id_Index = p.pdu_Index
JOIN AllStatus AS s ON i.Mi_Index = s.Mi_Index AND i.id_Index = s.status_Index
SELECT 'Shnugo: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100))
GO
Montewizdoh
DECLARE @d DATETIME = GETDATE()
SELECT
m.value('for $i in . return count(../../*[. << $i])', 'INT') AS message_info_position,
m.value('for $i in . return count(../*[. << $i]) + 1', 'INT') AS internal_position,
m.value('fn:local-name(.)', 'SYSNAME') AS element_name,
m.value('.', 'NVARCHAR(4000)') AS element_value
FROM #xml x
CROSS APPLY data.nodes('/response/message_infos/message_info/*') AS A(m)
SELECT 'Montewhizdoh: ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100))
GO
Devart (OLD)
DECLARE @d DATETIME = GETDATE()
SELECT
id = MAX(CASE WHEN name = 'id' THEN val END)
, pdu_id = MAX(CASE WHEN name = 'pdu_id' THEN val END)
, [status] = MAX(CASE WHEN name = 'status' THEN val END)
FROM (
SELECT
name = t.c.value('local-name(.)', 'SYSNAME')
, val = t.c.value('.', 'INT')
, RowNum = ROW_NUMBER() OVER (ORDER BY 1/0)
, RowNumGroup = ROW_NUMBER() OVER (ORDER BY 1/0) % 3
FROM #xml x
CROSS APPLY data.nodes('/response/message_infos/message_info/*') t(c)
) t
GROUP BY RowNum - CASE WHEN RowNumGroup = 0 THEN 3 ELSE RowNumGroup END
SELECT 'Devart (OLD): ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100))
GO
Devart (NEW)
DECLARE @d DATETIME = GETDATE()
SELECT
id = MIN(CASE WHEN RowNumGroup = 1 THEN val END)
, pdu_id = MIN(CASE WHEN RowNumGroup = 2 THEN val END)
, [status] = MIN(CASE WHEN RowNumGroup = 0 THEN val END)
FROM (
SELECT
val = t.c.value('(./text())[1]', 'BIGINT')
, RowNum = ROW_NUMBER() OVER (ORDER BY 1/0)
, RowNumGroup = ROW_NUMBER() OVER (ORDER BY 1/0) % 3
FROM #xml x
CROSS APPLY data.nodes('/response/message_infos/message_info/*') t(c)
) t
GROUP BY RowNum - CASE WHEN RowNumGroup = 0 THEN 3 ELSE RowNumGroup END
SELECT 'Devart (NEW): ' + CAST(CAST(GETDATE()-@d AS TIME) AS VARCHAR(100))
Performance comparison (Core i5 4460 3.2GHz
, DDR3 8Gb
, SQL Server 2014 SP1 Express
):
Mikael Eriksson: 00:00:00.327
Shnugo: 00:00:00.913
Montewhizdoh: 00:00:01.680
Devart (OLD): 00:00:00.363
Devart (NEW): 00:00:00.200
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With