我有这个变量:
declare @xmlDoc XML
它中存储了以下xml:
<?xml version="1.0" encoding="utf-8"?>
<NewDataSet>
<Table1>
<Sharedparam>shared</Sharedparam>
<Antoher>sahre</Antoher>
<RandomParam2>Good stuff</RandomParam2>
<MoreParam>and more</MoreParam>
<ResultsParam>2</ResultsParam>
</Table1>
<Table1>
<RandomParam2>do you</RandomParam2>
<MoreParam>think</MoreParam>
<ResultsParam>2</ResultsParam>
</Table1>
<Table1>
<Sharedparam>Last</Sharedparam>
<Antoher> Set </Antoher>
<RandomParam2> of </RandomParam2>
<MoreParam>values</MoreParam>
<ResultsParam>are here</ResultsParam>
</Table1>
<Table1 />
</NewDataSet>
我有这个用于获取数据的查询:
declare @xmlDoc XML
set @xmlDoc = '' -- Stack Overflow could not handle the xml all on one line.
SELECT -- Param 1
TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,
-- Param2
TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value,
-- Param3
TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
-- Param 4
TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
-- Param 5
TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
FROM @xmldoc.nodes('/NewDataSet/Table1') AS TBL(SParam)
我需要一种方法来向我的结果添加它们来自xml文件的顺序。 (这是Table1的第一个实例,然后是第二个......)。
出于SQL table variable limitations的原因,我不能使用标识列来保持这一点。 (出于其他原因,我不想使用临时表。)
我希望有一个很酷的SQL XML函数可以返回某种内部分配的节点ID。 (或其他一些类似的排序方式。)
注意,我不控制这个XML结构(我只是一个读者)所以我无法进行更改以添加ID属性。
任何建议都会很棒!
修改/更新
我真的希望得到这样的数据:
1 | SharedParam | shared 1 | Antoher | sahre 1 | RandomParam2 | Good stuff 1 | MoreParam | and more 1 | ResultsParam | and more 2 | RandomParam2 | do you 2 | MoreParam | think 2 | ResultsParam | 2 3 | Sharedparam | Last 3 | Antoher | Set . . .
但我很快就会出现。我可以将它列入列(或多或少),但我不知道如何编号。如果你有任何想法,我很乐意听到。
修改
我想出了这样做的查询(在互联网的帮助下)。它看起来像这样:
SELECT TBL.SParam.value('local-name(.)[1]', 'varchar(50)') as ParamName,
TBL.SParam.value('(.)[1]', 'varchar(50)') ParamValue,
TBL.SParam.value('for $s in . return count(../*[. << $s]) + 1', 'int') ParamPosition,
TBL.SParam.value('for $s in . return count(../../*[. << $s]) - 1', 'int') ParamIteration
FROM @xmldoc.nodes('/NewDataSet/Table1/*') AS TBL(SParam)
答案 0 :(得分:3)
您可以使用数字表格和position()
SELECT N.Number as ID,
-- Param 1
TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,
-- Param2
TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value,
-- Param3
TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
-- Param 4
TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
-- Param 5
TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
FROM master..spt_values as N
cross apply @xmldoc.nodes('/NewDataSet/Table1[position()=sql:column("N.Number")]') AS TBL(SParam)
where N.type = 'P' and
N.number between 1 and @xmlDoc.value('count(/NewDataSet/Table1)', 'int')
答案 1 :(得分:2)
查看此Fully support position() in xquery的连接请求请求者提供了一些可能对您有用的工作
基于第二种解决方法,我写了以下内容
SELECT
p.number,
TBL.SParam.value('local-name((*)[1])', 'varchar(50)') as Param1Name,
TBL.SParam.value('(*)[1]', 'varchar(100)') as Param1Value,
-- Param2
TBL.SParam.value('local-name((*)[2])', 'varchar(50)') as Param2Name,
TBL.SParam.value('(*)[2]', 'varchar(100)') as Param2Value,
-- Param3
TBL.SParam.value('local-name((*)[3])', 'varchar(50)') as Param3Name,
TBL.SParam.value('(*)[3]', 'varchar(100)') as Param3Value,
-- Param 4
TBL.SParam.value('local-name((*)[4])', 'varchar(50)') as Param4Name,
TBL.SParam.value('(*)[4]', 'varchar(100)') as Param4Value,
-- Param 5
TBL.SParam.value('local-name((*)[5])', 'varchar(50)') as Param5Name,
TBL.SParam.value('(*)[5]', 'varchar(100)') as Param5Value
FROM
master..spt_values p CROSS APPLY
@xmldoc.nodes('NewDataSet/Table1[position()=sql:column("p.number")]') AS TBL(SParam)
WHERE P.type = 'P'
返回以下内容
number Param1Name Param1Value Param2Name Param2Value Param3Name Param3Value Param4Name Param4Value Param5Name Param5Value
----------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ---------------------------------------------------------------------------------------------------- -------------------------------------------------- ----------------------------------------------------------------------------------------------------
1 Sharedparam shared Antoher sahre RandomParam2 Good stuff MoreParam and more ResultsParam 2
2 RandomParam2 do you MoreParam think ResultsParam 2 NULL NULL
3 Sharedparam Last Antoher Set RandomParam2 of MoreParam values ResultsParam are here
4 NULL NULL NULL NULL NULL
(4 row(s) affected)