gpt4 book ai didi

sql-server - Service Broker——如何从 XML 消息中提取行?

转载 作者:行者123 更新时间:2023-12-02 22:35:10 24 4
gpt4 key购买 nike

假设我有以下情况(用于演示)。简单表内容被转换为 XML 值并通过 Service Broker 发送到另一个 SQL 服务器,其中 SELECT 的结果将以非 XML 形式存储(即通常的简单数据库表) .让我们试试:

CREATE TABLE tab (a int, b int, c int);
GO

INSERT INTO tab (a, b, c) VALUES (1, 11, 111);
INSERT INTO tab (a, b, c) VALUES (2, 22, 222);
INSERT INTO tab (a, b, c) VALUES (3, 33, 333);
INSERT INTO tab (a, b, c) VALUES (4, 44, 444);
GO

SELECT * FROM tab FOR XML RAW, TYPE;
GO

在捕获消息的值时,它看起来像:

<row a="1" b="11" c="111" />
<row a="2" b="22" c="222" />
<row a="3" b="33" c="333" />
<row a="4" b="44" c="444" />

即单个多行字符串。比如说,我在目标机器上创建了完全相同的表结构:

CREATE TABLE tab_destination (a int, b int, c int);

@msg 中提取行的最佳方法是什么,以及如何将它们放入目标表?

谢谢,彼得

最佳答案

CREATE TABLE tab (a int, b int, c int); 
GO

INSERT INTO tab (a, b, c) VALUES (1, 11, 111);
INSERT INTO tab (a, b, c) VALUES (2, 22, 222);
INSERT INTO tab (a, b, c) VALUES (3, 33, 333);
INSERT INTO tab (a, b, c) VALUES (4, 44, 444);
GO

CREATE TABLE tab_destination (a int, b int, c int);
go

declare @x xml = (SELECT * FROM tab FOR XML RAW, TYPE);

insert into tab_destination (a, b, c)
select
x.value('@a', 'int'),
x.value('@c', 'int'),
x.value('@b', 'int')
from @x.nodes('//row') t(x);
GO

select * from tab_destination;
go

阅读时间 xml Data Type Methods

关于sql-server - Service Broker——如何从 XML 消息中提取行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11563452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com