SQL Server 2005 在子选择问题中使用并集选择 XML 路径

发布于 2024-08-17 20:57:54 字数 1535 浏览 4 评论 0原文

我对 SQL Server“选择 XML 路径”查询很有经验,但现在我遇到了一个奇怪的问题。

以下查询工作正常:

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from MyTable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')

这将在这个 XML 中产生(对于虚拟数据集):

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
  </Path>
</Root>

在这个(更大的)语句的结果中,我也需要第二个键字段:

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
    <Key Name="KeyField2">
      <Value>DummyValue2</Value>
    </Key>
  </Path>
</Root>

所以我用联合更改了我的(子)查询-选择:

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
     union all
     select
     'Keyfield2' as "@Name",
    t1.Keyfield2 as "Value"
    from MyTable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')

但现在我收到错误“当未使用 EXISTS 引入子查询时,只能在选择列表中指定一个表达式。”

我知道子查询中可以有多个记录XML 路径会产生多个元素。但我不明白为什么这不能通过工会来完成。

有人可以告诉我如何在我的(子)查询中使用 2 个关键字段来完成 XML 吗?

非常感谢你。

I'm rather experienced with SQL server "select for XML path" queries but now i run into a strange problem.

The following query works fine:

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from MyTable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')

This will result (for a dummy dataset) in this XML:

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
  </Path>
</Root>

In my result of this (part of a bigger) statement i need the 2nd keyfield too:

<Root>  
  <Path>
    <Key Name="KeyField1">
      <Value>DummyValue1</Value>
    </Key>
    <Key Name="KeyField2">
      <Value>DummyValue2</Value>
    </Key>
  </Path>
</Root>

So i changed my (sub)query with a union-select to:

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
     union all
     select
     'Keyfield2' as "@Name",
    t1.Keyfield2 as "Value"
    from MyTable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from MyTable t2
for XML path('Path') , elements XSINIL, root('Root')

But now i get the error "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."

I know it is possible to have multiple records in a subquery with for XML path witch results in multiple elements. But i don't understand why this can't be done with a union.

Can someone put me in the right direction how to accomplisch the XML with the 2 keyfields in my (sub)query?

Thanx you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-08-24 20:57:54

子选择的问题在于第一部分根本不引用任何表(没有 FROM- 子句)。

此清单为我提供了您请求的输出:

declare @mytable table (
keyfield1 nvarchar(20),
keyfield2 nvarchar(20)
)

insert into @mytable values ('Dummyvalue1', 'Dummyvalue2')
select * from @mytable

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from @mytable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from @mytable t2
for XML path('Path') , elements XSINIL, root('Root')


select 
(
    select * from (
      select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from @MyTable t1
    where 
    t1.KeyField1= t2.KeyField1
     union all
     select
     'Keyfield2' as "@Name",
    t3.Keyfield2 as "Value"
    from @MyTable t3
    where 
    t3.KeyField2= t2.KeyField2) a
    for xml path('Field'),type, elements 
) as 'Key'
from @MyTable t2
for XML path('Path') , elements XSINIL, root('Root')

The problem with your subselect is that the first part isn't referring to any table at all (no FROM-clause).

This listing gives me the output you requested:

declare @mytable table (
keyfield1 nvarchar(20),
keyfield2 nvarchar(20)
)

insert into @mytable values ('Dummyvalue1', 'Dummyvalue2')
select * from @mytable

select 
(
     select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from @mytable t1
    where 
    t1.KeyField1= t2.KeyField1 and
    t1.KeyField2= t2.KeyField2
    for xml path('Field'),type, elements 
) as 'Key'
from @mytable t2
for XML path('Path') , elements XSINIL, root('Root')


select 
(
    select * from (
      select
     'Keyfield1' as "@Name",
    t1.Keyfield1 as "Value"
    from @MyTable t1
    where 
    t1.KeyField1= t2.KeyField1
     union all
     select
     'Keyfield2' as "@Name",
    t3.Keyfield2 as "Value"
    from @MyTable t3
    where 
    t3.KeyField2= t2.KeyField2) a
    for xml path('Field'),type, elements 
) as 'Key'
from @MyTable t2
for XML path('Path') , elements XSINIL, root('Root')
小…楫夜泊 2024-08-24 20:57:54

这是一个简化的示例,但这能满足您的需要吗?

select 
    (
        select
            'Keyfield1' as "@Name",
            'Blah' as "Value"
        for xml path('Key'),type, elements 
    ),
    (
        select
            'Keyfield2' as "@Name",
            'Blah' as "Value"
        for xml path('Key'),type, elements 
    )
for XML path('Path') , elements XSINIL, root('Root')

Here is a simplified example, but does this get you what you need?

select 
    (
        select
            'Keyfield1' as "@Name",
            'Blah' as "Value"
        for xml path('Key'),type, elements 
    ),
    (
        select
            'Keyfield2' as "@Name",
            'Blah' as "Value"
        for xml path('Key'),type, elements 
    )
for XML path('Path') , elements XSINIL, root('Root')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文