我想解析这个Xml以获得以下结果。表的名称是SchoolRecord
Name Answer
School name 87f6c8bf-cafc-40fb-a082-ca9d5bfaf1e0
Course 2f23e1cb-181e-4af2-a9ec-3dd68530d1d5
Father NULL
Mother NULL
我正在使用SQL Server 2012.这是我尝试过的但是没有用
1
Select
S.userdefinedxml.value('(/ControlGroup/UserDefinedControls/Control/Name)[1]','varchar(max)' ) as Name,
S.userdefinedxml.value('(/ControlGroup/UserDefinedControls/Control/Answer)[1]','varchar(max)' ) as Answer
From SchoolRecord S
2
Select
S.userdefinedxml.value('(School_Data/ControlGroup/UserDefinedControls/Control/Name)[1]','varchar(max)' ) as Name,
S.userdefinedxml.value('(School_Data/ControlGroup/UserDefinedControls/Control/Answer)[1]','varchar(max)' ) as Answer
From SchoolRecord S
3
Select
S.userdefinedxml.value('(Data/School_Data/ControlGroup/UserDefinedControls/Control/Name)[1]','varchar(max)' ) as Name,
S.userdefinedxml.value('(Data/School_Data/ControlGroup/UserDefinedControls/Control/Answer)[1]','varchar(max)' ) as Answer
From SchoolRecord S
我的结果
Name Answer
NULL NULL
NULL NULL
NULL NULL
NULL NULL
我的XML:
<data>
<School_Data>
<ControlGroup>
<UserDefinedControls>
<Control>
<ControlType>FIND</ControlType>
<Name>School name</Name>
<Answer>87f6c8bf-cafc-40fb-a082-ca9d5bfaf1e0</Answer>
</Control>
</UserDefinedControls>
<UserDefinedControls>
<Control>
<ControlType>FIND</ControlType>
<Name>Course</Name>
<Answer>2f23e1cb-181e-4af2-a9ec-3dd68530d1d5</Answer>
</Control>
</UserDefinedControls>
<UserDefinedControls>
<Control>
<ControlType>FIND</ControlType>
<Name>Father</Name>
<Answer />
</Control>
</UserDefinedControls>
<UserDefinedControls>
<Control>
<ControlType>FIND</ControlType>
<Name>Mother</Name>
<Answer />
</Control>
</UserDefinedControls>
</ControlGroup>
</School_Data>
</data>
答案 0 :(得分:0)
我看到Yuck提到的元素和属性名称区分大小写。下面是一个返回每个控件的名称和答案的示例:
SELECT
Control.value('Name[1]','varchar(max)' ) as Name,
Control.value('Answer[1]','varchar(max)' ) as Answer
FROM dbo.SchoolRecord S
CROSS APPLY S.userdefinedxml.nodes('/data/School_Data/ControlGroup/UserDefinedControls/Control') AS UserDefinedControls(Control);