如何查询行及其 xml 表示形式?

发布于 2024-08-09 03:36:21 字数 709 浏览 2 评论 0原文

我需要类似的东西

select * from tb_listings for xml auto

,但我需要每一行都是分开的,而不是一个大的 xml 文档。

我尝试过类似以下的操作:

select id, (select * from tb_listings a where a.id=id for xml auto) as xmldata from tb_listings

预期输出如下:

id         xmldata 
------------------------------------------------------------
 1         <listing><name>ABC</name><xyz>123</xyz></listing>

但它似乎没有达到我想要的效果,而且还需要很长时间才能运行。

任何想法将不胜感激。 :)

编辑:想通了:

select id, (select top 1 * from tb_listings a where a.id=b.id for xml auto) from tb_listings b 

结束。

I need something like

select * from tb_listings for xml auto

But I need every row to be separate, and not one big xml document.

I have tried something like the following:

select id, (select * from tb_listings a where a.id=id for xml auto) as xmldata from tb_listings

Expected output is like:

id         xmldata 
------------------------------------------------------------
 1         <listing><name>ABC</name><xyz>123</xyz></listing>

But it doesn't seem to do what I want and it also takes a very long time to run.

Any ideas would be appreciated. :)

Edit: Figured it out:

select id, (select top 1 * from tb_listings a where a.id=b.id for xml auto) from tb_listings b 

Closing.

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

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

发布评论

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

评论(1

野稚 2024-08-16 03:36:21

尝试这样的事情:

DECLARE @YourTable table (PK1 int, c1 int, c2 varchar(5), c3 datetime)
INSERT INTO @YourTable VALUES (1,2,'abcde','1/1/2009')
INSERT INTO @YourTable VALUES (100,200,'zzz','12/31/2009 23:59:59')

--list all columns in xml format
SELECT
    t2.PK1  --optional, can remove this column from the result set and just get the XML
        ,(SELECT
              *
              FROM @YourTable  t1
              WHERE t1.PK1= t2.PK1
              FOR XML PATH('YourTable'), TYPE
         ) as Row
    FROM @YourTable  t2

输出:

PK1         Row
----------- ------------------------------------------------------------------------------------------
1           <YourTable><PK1>1</PK1><c1>2</c1><c2>abcde</c2><c3>2009-01-01T00:00:00</c3></YourTable>
100         <YourTable><PK1>100</PK1><c1>200</c1><c2>zzz</c2><c3>2009-12-31T23:59:59</c3></YourTable>

(2 row(s) affected)

try something like this:

DECLARE @YourTable table (PK1 int, c1 int, c2 varchar(5), c3 datetime)
INSERT INTO @YourTable VALUES (1,2,'abcde','1/1/2009')
INSERT INTO @YourTable VALUES (100,200,'zzz','12/31/2009 23:59:59')

--list all columns in xml format
SELECT
    t2.PK1  --optional, can remove this column from the result set and just get the XML
        ,(SELECT
              *
              FROM @YourTable  t1
              WHERE t1.PK1= t2.PK1
              FOR XML PATH('YourTable'), TYPE
         ) as Row
    FROM @YourTable  t2

OUTPUT:

PK1         Row
----------- ------------------------------------------------------------------------------------------
1           <YourTable><PK1>1</PK1><c1>2</c1><c2>abcde</c2><c3>2009-01-01T00:00:00</c3></YourTable>
100         <YourTable><PK1>100</PK1><c1>200</c1><c2>zzz</c2><c3>2009-12-31T23:59:59</c3></YourTable>

(2 row(s) affected)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文