2

私が次の状況にあるとしましょう(デモンストレーション用)。単純なテーブルの内容は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それらを宛先テーブルに配置する方法は何ですか?

ありがとう、ペトル

4

2 に答える 2

4
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データ型メソッドを読む時間

于 2012-07-19T15:00:41.793 に答える
1

その他のオプション (ただし、Remus Rusanu の例を好むと思います..多くの列とテーブル構造が同じ場合、これは怠惰になるのに役立ちます):

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

Declare @docHandle int  
EXEC sp_xml_preparedocument @docHandle OUTPUT, @x

Select *
FROM OPENXML(@docHandle, 'tab//', 1) 
With dbo.Tab

EXEC sp_xml_removedocument @docHandle
于 2012-07-19T15:12:46.930 に答える