【问题标题】:Selecting ordinary columns and columns formatted as XML in the same query在同一查询中选择普通列和格式化为 XML 的列
【发布时间】:2021-09-15 22:12:27
【问题描述】:

如果我运行 select columnA from tableA,我会从表中获取来自 columnA 的值。

如果我运行select columnB, columnC from tableA as xml path, root,我会从 2 列中获取数据,格式为 XML。

如何将上述 2 个查询的效果合并为一个,以便在标记为 myXml 的单独列中以 XML 形式返回 columnAcolumnBcolumnC

我在 SQL2008R2 服务器上运行它。

【问题讨论】:

    标签: sql-server xml sql-server-2008


    【解决方案1】:
    with myTable as (
        select a.columnA,
        (select b.columnA, b.columnB 
            from tableA b
            where b.columnA = a.columnA
            for xml path, root
        ) as myXml
        from tableA a
    )
    select * from myTable
    

    我被 XML 迷住了,以至于我没有看到我需要在表上使用 2 个别名进行自联接!

    【讨论】:

    • 你好@DFlat。请将您的帖子标记为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2020-10-05
    • 2015-08-22
    • 2014-01-31
    • 1970-01-01
    • 2017-05-30
    • 1970-01-01
    相关资源
    最近更新 更多