我的Xml看起来像这样:
biometrictDate, biometricID,dateOfBirth, firstName, gender,
lastName, consumerUserId, MedicalHeightValue
是表格中的所有列。
<Assessment biometrictDate="20120305 08:03:00" biometricID="74330759"
dateOfBirth="1975-04-08" firstName="BRYAN" gender="M" lastName="HAYES"
consumerUserId="120004223500">
<HealthAttribute>
<Identifier>MedicalHeightValue</Identifier>
<Value>67</Value>
</HealthAttribute>
</Assessment>
MedicalHeightValue
应单独放在使用以下查询完成的HealthAttribute
代码之间:
select C.Value, C.Identifier
from TableA
outer apply (values
('MedicalHeightValue', MedicalHeightValue) ) as C(Identifier, Value)
for xml path('HealthAttribute')
现在我想在评估标记
中单独使用以下列{biometrictDate, biometricID, dateOfBirth, firstName, gender, lastName, consumerUserId}
请帮忙吗?
新XML应如下所示:
<Assessment biometrictDate="20120305 08:03:00" biometricID="74330759"
dateOfBirth="1975-04-08" firstName="BRYAN" gender="M" lastName="HAYES"
consumerUserId="120004223500">
<HealthAttribute>
<Identifier>MedicalHeightValue</Identifier>
<Value>67</Value>
</HealthAttribute>
</Assessment>
答案 0 :(得分:0)
首先,真的很难理解你想要得到什么。我想我已经从你的问题和你的pervious question中找到了这个问题(顺便说一句,BTW仍然没有接受答案)。
以下是您需要的查询:
select
A.biometrictDate, A.biometricID, A.dateOfBirth,
A.firstName, A.gender, A.lastName, A.consumerUserId,
(
select *
from (values
('MedicalHeightValue', A.MedicalHeightValue),
('MedicalWeightValue', A.MedicalWeightValue)
) as V(Identifier, Value)
for xml path('HealthAttribute'), type
)
from table1 as A
for xml raw('Assessment')
您也可以这样做,以便更好地控制名称:
select
A.biometrictDate as [@biometrictDate],
A.biometricID as [@biometricID],
A.dateOfBirth as [@dateOfBirth],
A.firstName as [@firstName],
A.gender as [@gender],
A.lastName as [@lastName],
A.consumerUserId as [@consumerUserId],
(
select *
from (values
('MedicalHeightValue', A.MedicalHeightValue),
('MedicalWeightValue', A.MedicalWeightValue)
) as V(Identifier, Value)
for xml path('HealthAttribute'), type
)
from table1 as A
for xml path('Assessment')
<强> sql fiddle demo 强>