SQL“FOR XML PATH”返回DISTINCT错误

时间:2017-07-03 13:47:25

标签: sql xml

我是一个SQL新手。但是我已经成功地在XML中获得了一些东西。

这有效:

SELECT
    'update' AS                 "@mode"
    ,'DN_Dimension' AS          CS_FT_ID
    ,'DN Dimension' AS          "CS_FT_NAMES/CS_FT_NAME"
    ,'string' AS                "CS_FT_CONTENT/CS_FT_DATATYPE"
    ,'multivalent' AS           "CS_FT_CONTENT/CS_FT_VALENCY"
    ,(SELECT
          DNDim.Code AS             "CS_FT_VALUE_ID"
          ,CASE WHEN DNDim.Description='' THEN DNDim.Code ELSE DNDim.Description END "CS_FT_DATA"
      FROM 
          [Master Data Repository$DN Dimension] DNDim
      FOR XML PATH ('CS_FT_VALUE'), ROOT ('CS_FT_VALUES'), TYPE)
FOR XML PATH ('CS_FEATURE_TEMPLATE'), ROOT ('CS_FEATURE_TEMPLATES');

但我想从更多表中获取UNION数据。所以我尝试了这个:

SELECT
'update' AS                 "@mode"
,'DN_Dimension' AS          CS_FT_ID
,'DN Dimension' AS          "CS_FT_NAMES/CS_FT_NAME"
,'string' AS                "CS_FT_CONTENT/CS_FT_DATATYPE"
,'multivalent' AS           "CS_FT_CONTENT/CS_FT_VALENCY"
,(SELECT
DNDim.Code AS               "CS_FT_VALUE_ID"
,CASE WHEN DNDim.Description='' THEN DNDim.Code ELSE DNDim.Description END "CS_FT_DATA"
FROM [Master Data Repository$DN Dimension] DNDim
FOR XML PATH ('CS_FT_VALUE'), ROOT ('CS_FT_VALUES'),TYPE
)
UNION
SELECT
    'update' AS                 "@mode"
    ,'Max_Pressure' AS          CS_FT_ID
    ,'Max. Operating Pressure' AS "CS_FT_NAMES/CS_FT_NAME"
    ,'string' AS                "CS_FT_CONTENT/CS_FT_DATATYPE"
    ,'multivalent' AS           "CS_FT_CONTENT/CS_FT_VALENCY"
    ,(SELECT
    MaxOP.Code AS               "CS_FT_VALUE_ID"
    ,CASE WHEN MaxOP.Description='' THEN MaxOP.Code ELSE MaxOP.Description END "CS_FT_DATA"
    FROM [Master Data Repository$Max_ Operating Pressure] MaxOP
    FOR XML PATH ('CS_FT_VALUE'), ROOT ('CS_FT_VALUES'),TYPE
    )
FOR XML PATH ('CS_FEATURE_TEMPLATE'), ROOT ('CS_FEATURE_TEMPLATES');

现在我收到错误

  

不能将xml数据类型选为DISTINCT,因为它不具有可比性

即使我不在查询中使用DISTINCT。

我可以通过删除子查询FOR XML PATH中的TYPE来摆脱它们 - 但是子查询的元素不会返回正确。

有什么想法吗?

0 个答案:

没有答案