Use XQuery to get at this data

社会主义新天地 提交于 2019-12-05 18:28:04

Try something like this:

SELECT
    TBL.SParam.value('(.)[1]', 'varchar(50)')
FROM
    @xmldoc.nodes('/NewDataSet/Table1/Sharedparam') AS TBL(SParam)

Gives me an output of:

(No column name)
shared
Hey

Update: if you want to get at all the XML elements and their values inside the <Table1> elements, you can use this XQuery:

SELECT
    TBL.SParam.value('local-name(.)[1]', 'varchar(50)') 'Attribute',
    TBL.SParam.value('(.)[1]', 'varchar(50)') 'Value'
FROM
    @xmldoc.nodes('/NewDataSet/Table1/*') AS TBL(SParam)

which outputs:

Attribute            Value
Sharedparam          shared
Antoher              sahre
RandomParam2         Good stuff
MoreParam            and more
ResultsParam         2
Sharedparam          Hey
Antoher              what 
RandomParam2         do you
MoreParam            think
ResultsParam         2

Update #2: to get the values of the first <Table1> and the second <Table1> XML node next to one another, you need to do two calls to .nodes() - once retrieving the first node, the other time the second one. It gets a bit hairy, especially if you want to extend that even further - and performance is going to be abysmal - but it works :-)

SELECT
    TBL.SParam.value('local-name(.)[1]', 'varchar(50)') 'Attribute',
    TBL.SParam.value('(.)[1]', 'varchar(50)') 'Value 1',
    TBL2.SParam2.value('(.)[1]', 'varchar(50)') 'Value 2'
FROM
    @xmldoc.nodes('/NewDataSet/Table1[1]/*') AS TBL(SParam)
INNER JOIN
    @xmldoc.nodes('/NewDataSet/Table1[2]/*') AS TBL2(SParam2) ON TBL.SParam.value('local-name(.)[1]', 'varchar(50)') = TBL2.SParam2.value('local-name(.)[1]', 'varchar(50)')

Gives an output of:

Attribute      Value 1     Value 2
Sharedparam    shared       Hey
ResultsParam      2          2
RandomParam2   Good stuff   do you
Antoher        sahre        what 
MoreParam      and more     think

That's an odd layout. You expect columns Sharedparam,Antoher etc: not rows.

And if I read that right, Table1 maxOccurs="unbounded" which means an variable number of columns = not SQL which is fixed column type and number

To read each tag as a column (which are fixed and finite) you'd do this:

SELECT
   x.item.value('(Sharedparam)[1]', 'varchar(100)') AS Sharedparam,
   x.item.value('(Antoher)[1]', 'varchar(100)') AS Antoher,
   x.item.value('(SharedRandomParam2param)[1]', 'varchar(100)') AS RandomParam2,
   ...
FROM
   @xmlDoc.nodes('/NewDataSet/Table1') x(item)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!