[英]XML tag formation using sql query
My Xml Looks like this: 我的Xml看起来像这样:
biometrictDate, biometricID,dateOfBirth, firstName, gender,
lastName, consumerUserId, MedicalHeightValue
are all columns from a table. 是表中的所有列。
<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
should alone be placed in between HealthAttribute
tags which is completed using the following query: MedicalHeightValue
应该单独放在HealthAttribute
标记之间,可使用以下查询完成:
select C.Value, C.Identifier
from TableA
outer apply (values
('MedicalHeightValue', MedicalHeightValue) ) as C(Identifier, Value)
for xml path('HealthAttribute')
Now I want the following columns alone in Assessment tag 现在,我只想在评估标签中添加以下列
{biometrictDate, biometricID, dateOfBirth, firstName, gender, lastName, consumerUserId}
Any help please? 有什么帮助吗?
New XML should look like this: 新的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>
First of all, it's REALLY hard to understand what do you want to get. 首先,很难理解您想要获得什么。 I think I've figured this out from your question and your pervious question (which, BTW, still don't have an accepted answer).
我想我已经从您的问题和您以前的问题 (顺便说一句,仍然没有被接受的答案)中弄清楚了。
Here's query you need: 这是您需要的查询:
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')
you can also do it like this, to have more control over names: 您也可以这样做,以更好地控制名称:
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.