1

I have the following SQL,

SELECT AT.ID ATT_ID
      ,VAL.ID VAL_ID
      ,COALESCE(ATT.Name,AT.Name) AS Name
      ,COALESCE(VALT.Value,VAL.Value) AS Value
      ,COALESCE(GT.Name,G.Name) AS GroupName
      ,AT.KeyAttribute
      ,AT.[Order]
FROM    Attributes AT
  LEFT OUTER JOIN AttributesTranslations ATT on (AT.ID = ATT.AttributeID AND ATT.LanguageID = @LanguageID)
  LEFT OUTER JOIN AttributesValues VAL ON VAL.AttributeID = AT.ID AND VAL.ProductID = @ProductID
  LEFT OUTER JOIN AttributesValuesTranslations VALT on (VAL.ID = VALT.AttributeValueID AND VALT.LanguageID = @LanguageID)
  LEFT OUTER JOIN AttributesGroups G ON G.ID = AT.GroupID
  LEFT OUTER JOIN AttributesGroupsTranslations GT on (G.ID = GT.AttributeGroupID AND GT.LanguageID = @LanguageID)
WHERE   AT.ProductTypeID = (SELECT ProductTypeID FROM Products WHERE ID = @ProductID)
    AND ((AT.RetailerID = @RetailerID) OR (@RetailerID = -1))
ORDER BY G.[order],G.Name ,AT.[Order]
FOR XML AUTO, ELEMENTS

It will return,

<AT>
  <ATT_ID>2</ATT_ID>
  <KeyAttribute>0</KeyAttribute>
  <Order>2</Order>
  <VAL>
    <VAL_ID>32614</VAL_ID>
    <Name>Design</Name>
    <Value>asfdsa</Value>
    <GroupName>zOther</GroupName>
  </VAL>
</AT>
<AT>
  <ATT_ID>2</ATT_ID>
  <KeyAttribute>0</KeyAttribute>
  <Order>2</Order>
  <VAL>
    <VAL_ID>32614</VAL_ID>
    <Name>Design</Name>
    <Value>asfdsa</Value>
    <GroupName>zOther</GroupName>
  </VAL>
</AT>

I want the result as,

<AT>
  <ATT_ID>2</ATT_ID>
  <KeyAttribute>0</KeyAttribute>
  <Order>2</Order>
    <VAL_ID>32614</VAL_ID>
    <Name>Design</Name>
    <Value>asfdsa</Value>
    <GroupName>zOther</GroupName>
</AT>
<AT>
  <ATT_ID>2</ATT_ID>
  <KeyAttribute>0</KeyAttribute>
  <Order>2</Order>

    <VAL_ID>32614</VAL_ID>
    <Name>Design</Name>
    <Value>asfdsa</Value>
    <GroupName>zOther</GroupName>

</AT>
4

2 回答 2

3

不确定它是否会起作用,因为我没有要测试的表结构,但它在我的简化演示中给出了结果。尝试将您的查询包装在 CTE(或子查询)中并SELECT ... FOR XML在其上使用:

WITH AT AS 
(
    SELECT AT.ID ATT_ID
      ,VAL.ID VAL_ID
      ,COALESCE(ATT.Name,AT.Name) AS Name
      ,COALESCE(VALT.Value,VAL.Value) AS Value
      ,COALESCE(GT.Name,G.Name) AS GroupName
      ,AT.KeyAttribute
      ,AT.[Order]
      ,G.Order as G_Order --added for ordering
      ,G.Name as G_Name --added for ordering
    FROM    Attributes AT
      LEFT OUTER JOIN AttributesTranslations ATT on (AT.ID = ATT.AttributeID AND ATT.LanguageID = @LanguageID)
      LEFT OUTER JOIN AttributesValues VAL ON VAL.AttributeID = AT.ID AND VAL.ProductID = @ProductID
      LEFT OUTER JOIN AttributesValuesTranslations VALT on (VAL.ID = VALT.AttributeValueID AND VALT.LanguageID = @LanguageID)
      LEFT OUTER JOIN AttributesGroups G ON G.ID = AT.GroupID
      LEFT OUTER JOIN AttributesGroupsTranslations GT on (G.ID = GT.AttributeGroupID AND GT.LanguageID = @LanguageID)
    WHERE   AT.ProductTypeID = (SELECT ProductTypeID FROM Products WHERE ID = @ProductID)
       AND ((AT.RetailerID = @RetailerID) OR (@RetailerID = -1))
)
SELECT 
    ATT_ID ,
    KeyAttribute,
    [Order],
    VAL_ID ,
    Name ,
    Value ,
    GroupName  
FROM AT
ORDER BY G_Order,G_Name,[Order]
FOR XML AUTO, ELEMENTS

简化演示

于 2013-07-16T12:39:57.903 回答
2

这是一个简化(丑化)的版本:将所有内容包装在一个选择中并执行 FOR XML :

SELECT * FROM (
  ...your-select-here...
) temp
FOR XML AUTO, ELEMENTS
于 2013-07-16T12:50:00.007 回答