SQL Server查询名称值对

时间:2016-10-26 18:03:19

标签: sql sql-server xml tsql for-xml

我是使用SQL Server创建XML文件的新手。我有2张桌子; 1为人,另一为默认设置。

表:

ABC   
DEF

设置表:

Code           name                 value           
-----------------------------------------
Hair           Hair color           Brown                  
Hair           Texture              Curly             
Shoes          Shoe size            6            
Shoes          Shoe color           White    

如何创建看起来像这样的名称值对?当我尝试获取设置时,收到有关多个记录的错误:

<PersonData>
  <Person>ABC</Person>
    <DefaultSettings>
      <Code>Hair</Code>
      <settings>
        <name>Hair color</name>
        <value>Brown</value>
      </settings>
      <settings>
        <name>Texture</name>
        <value>Curly</value>
      </settings>
      <Code>Shoes</Code>
      <settings>
        <name>Shoe size</name>
        <value>6</value>
      </settings>
      <settings>
        <name>Shoe color</name>
        <value>White</value>
      </settings>
    </DefaultSettings>
  </Person>
  <Person>DEF</Person>
    <DefaultSettings>
      <Code>Hair</Code>
      <settings>
        <name>Hair color</name>
        <value>Brown</value>
      </settings>
      <settings>
        <name>Texture</name>
        <value>Curly</value>
      </settings>
      <Code>Shoes</Code>
      <settings>
        <name>Shoe size</name>
        <value>6</value>
      </settings>
      <settings>
        <name>Shoe color</name>
        <value>White</value>
      </settings>
    </DefaultSettings>
  </Person>
</PersonData>

1 个答案:

答案 0 :(得分:1)

正如我在评论中所说,您给定的XML无效。以下查询接近您请求的输出。希望,我的魔法玻璃灯泡还在工作:

DECLARE @personTable TABLE(ID INT IDENTITY, Name VARCHAR(100));
INSERT INTO @personTable VALUES
('ABC'),('DEF');

DECLARE @defaultSettings TABLE(Code VARCHAR(100),name VARCHAR(100),value VARCHAR(100));           
INSERT INTO @defaultSettings VALUES 
 ('Hair','Hair color','Brown')                  
,('Hair','Texture','Curly')        
,('Shoes','Shoe size','6')            
,('Shoes','Shoe color','White');

SELECT pd.ID AS [Person/@id]
      ,pd.Name AS [Person/@name]
      ,(
        SELECT ds.Code
              ,(
                SELECT ds2.name
                      ,ds2.value
                FROM @defaultSettings AS ds2
                WHERE ds.Code=ds2.Code
                FOR XML PATH('settings'),TYPE
               )
        FROM @defaultSettings AS ds
        GROUP BY ds.Code
        FOR XML PATH(''),TYPE
       ) AS [Person/DefaultSettings]
FROM @personTable AS pd
FOR XML PATH(''),ROOT ('PersonData')  

结果

<PersonData>
  <Person id="1" name="ABC">
    <DefaultSettings>
      <Code>Hair</Code>
      <settings>
        <name>Hair color</name>
        <value>Brown</value>
      </settings>
      <settings>
        <name>Texture</name>
        <value>Curly</value>
      </settings>
      <Code>Shoes</Code>
      <settings>
        <name>Shoe size</name>
        <value>6</value>
      </settings>
      <settings>
        <name>Shoe color</name>
        <value>White</value>
      </settings>
    </DefaultSettings>
  </Person>
  <Person id="2" name="DEF">
    <DefaultSettings>
      <Code>Hair</Code>
      <settings>
        <name>Hair color</name>
        <value>Brown</value>
      </settings>
      <settings>
        <name>Texture</name>
        <value>Curly</value>
      </settings>
      <Code>Shoes</Code>
      <settings>
        <name>Shoe size</name>
        <value>6</value>
      </settings>
      <settings>
        <name>Shoe color</name>
        <value>White</value>
      </settings>
    </DefaultSettings>
  </Person>
</PersonData>