我找到了一些类似的例子,但我无法让它发挥作用。
我已将XML数据插入到表中,需要一个示例来查询Param Name和Values,其中Module Name =“P2”
XML数据看起来像这样。
<TrendProject>
<Site CncAddress="81" TuaString="T/IQ3 - 00:10:70:00:8B:26/TCP,,,10.17.250.105,10081///13/21/">
<Lan IsLocal="True" LanNumber="13">
<Device DeviceNumber="21" VersionString="IQ3xcite128 Iss3.06 Jan 30 2014">
<DeviceOverview DeviceType="q" MajorVersion="3" MinorVersion="0" HexEncoded="711B1B1B41333036" Revision="6"></DeviceOverview>
<Module Name="P1" SubTypeNumber="-1">
<Param Name="$" Type="2" Value="Udetemperatur" />
<Param Name="S" Type="1" Value="0.0" >
<ConEndPoint>S20V</ConEndPoint>
</Param>
<Param Name="P" Type="6" Value="4" Enum="5 Minutes" />
<Param Name="C" Type="6" Value="28732" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="B1" Type="300" >
<Param Name="V" Type="1" Value="0.0" />
<Param Name="Y" Type="6" Value="2" />
<Param Name="S" Type="108" Value="0" />
<Param Name="E" Type="6" Value="28732" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="O" Type="6" Value="0" />
</Param>
</Module>
<Module Name="P2" SubTypeNumber="-1">
<Param Name="$" Type="2" Value="BL01 MV 10.X.2 Varme Funktion" />
<Param Name="S" Type="1" Value="100.0" >
<ConEndPoint>S52V</ConEndPoint>
</Param>
<Param Name="P" Type="6" Value="4" Enum="5 Minutes" />
<Param Name="C" Type="6" Value="57617" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="B1" Type="300" >
<Param Name="V" Type="1" Value="100.0" />
<Param Name="Y" Type="6" Value="2" />
<Param Name="S" Type="108" Value="0" />
<Param Name="E" Type="6" Value="57617" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="O" Type="6" Value="0" />
</Param>
</Module>
<Module Name="P3" SubTypeNumber="-1">
<Param Name="$" Type="2" Value="BL01 TF10 Fremlob" />
<Param Name="S" Type="1" Value="21.0" >
<ConEndPoint>S1V</ConEndPoint>
</Param>
<Param Name="P" Type="6" Value="4" Enum="5 Minutes" />
<Param Name="C" Type="6" Value="57617" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="B1" Type="300" >
<Param Name="V" Type="1" Value="21.0" />
<Param Name="Y" Type="6" Value="2" />
<Param Name="S" Type="108" Value="64" />
<Param Name="E" Type="6" Value="57617" />
<Param Name="T" Type="7" Value="2014-05-30T09:00:00" />
<Param Name="O" Type="6" Value="0" />
</Param>
</Module>
</Device>
</Lan>
</Site>
</TrendProject>
我试图以很多方式查询XML并陷入困境。这只返回所有模块的所有属性。
DECLARE @myTable TABLE(plotXML XML)
INSERT INTO @myTable(plotXML) Values ((SELECT PlotsSettings From PlotsXMLdata WHERE iqID = 1))
SELECT * FROM @myTable
IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
DROP TABLE #Temp
SELECT TP2.Param.value('(@Name)', 'nvarchar(4)') as PlotParamName, TP2.Param.value('(@Value)', 'nvarchar(50)') as PlotParamValue, TP.Modules.value('(.)[1]', 'nvarchar(50)') as PlotModuleName INTO #Temp
FROM @myTable
CROSS APPLY
plotXML.nodes('/TrendProject/Site/Lan/Device/Module/@Name') as TP(Modules)
CROSS APPLY
plotXML.nodes('(/TrendProject/Site/Lan/Device/Module/Param)') as TP2(Param)
SELECT * FROM #Temp
答案 0 :(得分:0)
如果您的XML位于变量@xml
select
x.n.value('./@Name','varchar(50)') ,
x.n.value('./@Value','varchar(50)')
from @xml.nodes('//Module[@Name="P2"]/Param') x(n)
或从表格
select
x.n.value('./@Name','varchar(50)') ,
x.n.value('./@Value','varchar(50)')
from yourtable
cross apply
xmlField.nodes('//Module[@Name="P2"]/Param') x(n)