Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用sql查询生成XML标记_Sql_Sql Server_Xml_Sqlxml - Fatal编程技术网

使用sql查询生成XML标记

使用sql查询生成XML标记,sql,sql-server,xml,sqlxml,Sql,Sql Server,Xml,Sqlxml,我的Xml如下所示: biometrictDate, biometricID,dateOfBirth, firstName, gender, lastName, consumerUserId, MedicalHeightValue <Assessment biometrictDate="20120305 08:03:00" biometricID="74330759" dateOfBirth="1975-04-08" firstName="BRYAN" gen

我的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>
表中的所有列

<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>
现在,我想在评估标签中单独显示以下列

{biometrictDate, biometricID, dateOfBirth, firstName, gender, lastName, consumerUserId} 
需要帮忙吗

新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>

医学价值
67

首先,很难理解你想要得到什么。我想我已经从你的问题和你的(顺便说一句,仍然没有一个公认的答案)中找到了答案

以下是您需要的查询:

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')

您能告诉我们您的新XML应该是什么样子吗?从您的描述中不太清楚…它应该是这样的:MedicalHeightValue 67请不要将代码样本或样本数据放入注释中-因为您无法格式化它,所以阅读它非常困难。。。。取而代之的是:通过编辑你的问题来更新它,以提供额外的信息!谢谢。好的,我无法编辑我的原始帖子。问题清楚了吗?或者你需要其他信息吗?请告诉我,我看不出你显示的两组XML之间有任何区别。。。。。