我有一个我在SQL Server 2012中查询的示例XML架构。我有一个带有XML列的表。我希望在返回每一行时包含xml片段。
以下是现有查询和更详细的说明。
<dev:Doc xmlns:dev="http://www.w3.org/2001/XMLSchema" Number="0" SchemaVersion="0.1" Settings="Testing" Title="Ordering">
<dev:Forms FormId="A1">
<dev:A1 ItemNumber="1" ItemGuid="{F402C584-555E-4D07-8C35-E88889B9DA44}">
<dev:Codes>D</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Visitors>
<dev:Visitor Name="Dev01" Location="STLRF">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
<dev:Visitor Name="Dev02" Location="STLRF">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
<dev:Visitor Name="Dev03" Location="FGRTY">
<dev:Divisions>
<dev:Division Number="1" Name="TFR3" Usage="Monitor">
<dev:Description>Development Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="2" Name="DEF32" Usage="Monitor">
<dev:Description>Testing Fundamentals</dev:Description>
</dev:Division>
<dev:Division Number="3" Name="DEP13" Usage="None">
<dev:Description>Guided Fundamentals</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="FGY(14A)" />
</dev:Senders>
</dev:A1>
</dev:Forms>
<dev:Forms FormId="A2">
<dev:A2 ItemNumber="1" ItemGuid="{3563F33E-B03A-4859-850E-A87D35BD8562}">
<dev:Codes>C</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Remarks>Support</dev:Remarks>
<dev:Notes>Ready</dev:Notes>
<dev:Visitors>
<dev:Visitor Name="GHFF">
<dev:Divisions>
<dev:Division Number="0" Name="Trial" Usage="None">
<dev:FromLocation>LOPO</dev:FromLocation>
<dev:ToLocation>RDSS</dev:ToLocation>
<dev:Description>Rich Filter</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="W33R" />
</dev:Senders>
<dev:IsReady>true</dev:IsReady>
<dev:IsCall>false</dev:IsCall>
</dev:A2>
<dev:A2 ItemNumber="2" ItemGuid="{CCFB2D5D-A23E-412D-8541-536451873713}">
<dev:Codes>A</dev:Codes>
<dev:Required>true</dev:Required>
<dev:Informational>false</dev:Informational>
<dev:Remarks>Loader Ready</dev:Remarks>
<dev:Notes>Ready</dev:Notes>
<dev:Visitors>
<dev:Visitor Name="UDT">
<dev:Divisions>
<dev:Division Number="0" Name="Trial" Usage="None">
<dev:FromLocation>TYUJ</dev:FromLocation>
<dev:ToLocation>DETF</dev:ToLocation>
<dev:Description>Web Enhance</dev:Description>
</dev:Division>
</dev:Divisions>
</dev:Visitor>
</dev:Visitors>
<dev:Senders>
<dev:Sender Name="RJ4" />
</dev:Senders>
<dev:IsReady>true</dev:IsReady>
<dev:IsCall>false</dev:IsCall>
</dev:A2>
</dev:Forms>
</dev:Doc>
示例查询
;WITH XMLNAMESPACES (Default 'http://www.w3.org/2001/XMLSchema' )
SELECT
a.value('@Number[1]', 'int') as Number
,b.value('(@FormId)[1]', 'NVARCHAR(50)') Form
--,XmlDocument.query('Doc/Forms') as FormXml,
,c.value('@ItemGuid[1]', 'uniqueidentifier') as ItemGuid
,c.value('@ItemNumber[1]', 'INT') AS ItemNumber
,d.value('(@Name)[1]','nvarchar(50)') As Visitor
,d.value('(@Location)[1]','nvarchar(50)') As Location
,e.value('(@Name)[1]', 'NVARCHAR(50)') As Sender
FROM
XmlTable As X
CROSS APPLY XmlDocument.nodes('Doc') As aa(a)
CROSS APPLY a.nodes('Forms') As bb(b)
CROSS APPLY b.nodes('*') As cc(c)
CROSS APPLY c.nodes('Visitors/Visitor') as dd(d)
CROSS APPLY c.nodes('Senders/Sender') as ee(e)
我想要做的是在Form列之后包含xml片段,以显示它来自的片段。
我正在尝试这个
XmlDocument.query('Doc/Forms') as FormXml,
以上是从该元素返回,我只想返回与<dev:Forms FormId="A1">
相关的每一行的</dev:A1>
到A1
的所有内容。因此,当Form为A2
时,我想要A2
- /A2
&lt; ---之间的所有内容
答案 0 :(得分:1)
我相信你想得到的是b
“表”所引用的XML,不是吗?
试试这一行:
, b.query('.') as FormXml
这会让你想要你想要的吗?它应该为您提供<Forms>
节点的XML。