简体   繁体   English

用UNION组合不同的表列,用XML区分列名

[英]Combine different table columns with UNION and use XML to differentiate the column names

Though this works to return the right results,虽然这可以返回正确的结果,

SELECT r.KeyColumn as '@Key', t1.Key1 as 'Key1', t1.Col1 as 'Col1' 
FROM @resultset r
INNER JOIN Table1 t1 ON t1.Col1 = 'SomeCondition'
--FOR XML PATH('Column1')  -- This errors out when used with union, works as a seperate query.

UNION
SELECT r.KeyColumn as '@Key', t2.Key1 as 'Key2', t2.Col2 as 'Col2' 
FROM @resultset r
INNER JOIN Table2 t2 ON t2.Col1 = 'SomeCondition2'
--FOR XML PATH('Column2')  -- This errors out when used with union, works as a seperate query.

Results:结果:

@Key Key1  Col1
1   1  Table1Col1
1   1  Table2Col

The problem is that the results together doesn't differentiate between the different naming I'm trying to use, because of the UNION.问题在于,由于 UNION 的原因,结果并不能区分我尝试使用的不同命名。 How can I get both to display under one resultset but with different names?如何让两者都显示在一个结果集下但名称不同? I'm thinking XML to do this, but am unable to figure out how?我在想 XML 这样做,但我无法弄清楚怎么做?

What's an optimal way to combine different queries with different results/number of rows and put everything under one big XML?将不同查询与不同结果/行数组合并将所有内容放在一个大 XML 下的最佳方法是什么?

Ok, the best I could come up with is:好的,我能想到的最好的方法是:

SELECT r.KeyColumn as '@Key', t1.Key1 as 'Key1', t1.Col1 as 'Col1',  '' as 'Key2', '' as 'Col2'
          FROM @resultset r
    INNER JOIN Table1 t1 ON t1.Col1 = 'SomeCondition'
    --FOR XML PATH('Column1')  --Error

     UNION 

     SELECT r.KeyColumn as '@Key', '' AS 'Key1', '' as 'Col1', t2.Key1 as 'Key2', t2.Col2 as 'Col2' 
    FROM @resultset r
    INNER JOIN Table2 t2 ON t2.Col1 = 'SomeCondition2'
    --FOR XML PATH('Column2')  -- Error

Will give me results (again, as expected because of the Union)会给我结果(再次,正如预期的那样,因为联盟)

    @Key    Key1        Col1        Key2    Col2
---------------------------------------------------------------------------
    1       1           Table1Col1
    1                               1       Table2Col

I still want to get my results as an XML thus:

<Root>
<Column1 Key="1">
  <Key1>1</Key1>
  <Col1>Table1Col1</Col1>
</Column1>

<Column2 Key="1">
  <Key2>1</Key2>
  <Col2>Tabl2Col</Col2>
</Column2>
</Root>

or something on these lines:或以下内容:

 <Root Key="1">
    <Column1>
      <Key1>1</Key1>
      <Col1>Table1Col1</Col1>
    </Column1>

    <Column2>
      <Key2>1</Key2>
      <Col2>Tabl2Col</Col2>
    </Column2>
    </Root>

Are you looking for something like this?你在寻找这样的东西吗?

declare @T table 
(
  KeyColumn int,
  Col1 varchar(10),
  Col2 varchar(10)
)

insert into @T values(1, 'Col1', 'Col2')

select (
         select KeyColumn,
                Col1
         from @T
         for xml path('Query1'), type       
       ),
       (
         select KeyColumn,
                Col2
         from @T
         for xml path('Query2'), type       
       )
for xml path('root')

Result:结果:

<root>
  <Query1>
    <KeyColumn>1</KeyColumn>
    <Col1>Col1</Col1>
  </Query1>
  <Query2>
    <KeyColumn>1</KeyColumn>
    <Col2>Col2</Col2>
  </Query2>
</root>

You can't do what you want.你不能做你想做的事。 The UNION uses the column names from the first SELECT statement and unions the columns ijn the second (and subsequent) SELECTs. UNION 使用第一个 SELECT 语句中的列名,并联合第二个(和后续)SELECT 中的列。 In other words, the first SELECT names the columns, and the SELECTs that follow are just considered to be additional rows of those columns.换句话说,第一个 SELECT 命名列,而后面的 SELECT 仅被视为这些列的附加行。

If you want separate column names, use JOINS or multiple-table SELECTs instead.如果您想要单独的列名,请改用 JOINS 或多表 SELECT。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM