2017-06-02 6 views
3

おはよう!別のテーブルからXMLを取得

私は質問があります。 1つの結果XMLで2つのテーブルをどのように組み合わせるかを知ることはできません。

<Root> 
    <FirstTable ID="1" SomeField="rec1" /> 
    <FirstTable ID="2" SomeField="rec2" /> 
    <FirstTable ID="3" SomeField="rec3" /> 
    <FirstTable ID="4" SomeField="rec4" /> 
    <AnotherTable ID="1" SomeField="s106" AnotherField="here" /> 
    <AnotherTable ID="2" SomeField="s12" AnotherField="just" /> 
    <AnotherTable ID="3" SomeField="s13" AnotherField="sample" /> 
</Root> 

dbfiddle​​

新規発言(編集済み)John Cappellettiによって答え

が、すべてこの内部を置く必要が:ここ

はサンプル

DECLARE @t1 table (ID int identity(1,1), SomeField varchar(50)) 
DECLARE @t2 table (ID int identity(1,1), SomeField varchar(50), AnotherField varchar(50)) 

INSERT INTO @t1 (SomeField) VALUES ('rec1'),('rec2'),('rec3'),('rec4') 
INSERT INTO @t2 (SomeField,AnotherField) VALUES ('s106','here'),('s12','just'),('s13','sample') 

SELECT * FROM @t1 AS FirstTable 
SELECT * FROM @t2 AS AnotherTable 

募集結果であり、 3番目の表。ここで

は新しいコードです:

DECLARE @t1 table (ID int identity(1,1), tID int, SomeField varchar(50)) 
DECLARE @t2 table (ID int identity(1,1), tID int, SomeField varchar(50), AnotherField varchar(50)) 
DECLARE @t3 table (ID int identity(1,1), field1 varchar(50), field2 varchar(50)) 


INSERT INTO @t1 (tID,SomeField) VALUES (1,'rec1'),(1,'rec2'),(1,'rec3'),(1,'rec4') 
INSERT INTO @t2 (tID,SomeField,AnotherField) VALUES (1,'s106','here'),(1,'s12','just'),(1,'s13','sample') 
INSERT INTO @t3 (field1,field2) VALUES ('field1 Value','field2 Value') 

募集結果(最終的に):

<ThirdTable ID="1" field1="field1 Value" field2="field2 Value"> 
    <FirstTable ID="1" tID="1" SomeField="rec1" /> 
    <FirstTable ID="2" tID="1" SomeField="rec2" /> 
    <FirstTable ID="3" tID="1" SomeField="rec3" /> 
    <FirstTable ID="4" tID="1" SomeField="rec4" /> 
    <AnotherTable ID="1" tID="1" SomeField="s106" AnotherField="here" /> 
    <AnotherTable ID="2" tID="1" SomeField="s12" AnotherField="just" /> 
    <AnotherTable ID="3" tID="1" SomeField="s13" AnotherField="sample" /> 
</ThirdTable> 

答えて

3

を追加しました拡張質問のための

Select * 
     ,(select cast(
        isnull((Select * From @t1 for xml raw('FirstTable')),'') 
        +isnull((Select * From @t2 for xml raw('AnotherTable')),'') 
       as xml) 
     ) 
From @t3 for xml raw('ThirdTable') 

戻り

<ThirdTable ID="1" field1="field1 Value" field2="field2 Value"> 
    <FirstTable ID="1" tID="1" SomeField="rec1" /> 
    <FirstTable ID="2" tID="1" SomeField="rec2" /> 
    <FirstTable ID="3" tID="1" SomeField="rec3" /> 
    <FirstTable ID="4" tID="1" SomeField="rec4" /> 
    <AnotherTable ID="1" tID="1" SomeField="s106" AnotherField="here" /> 
    <AnotherTable ID="2" tID="1" SomeField="s12" AnotherField="just" /> 
    <AnotherTable ID="3" tID="1" SomeField="s13" AnotherField="sample" /> 
</ThirdTable> 
+0

うわー、それは素晴らしいです!再度、感謝します! @ArkadiyV。 –

+0

。もう一度、面白い構造を助けてくれることを嬉しく思います。私はこのようなものを採用することができるかもしれないと思う。 –

+0

はい、私は構造が最も最適であると分かったし、すべてのテーブルを分離したものやDataSetを得る。テーブル 't1'にレコードがない場合、テーブル 't2'も追加されません。興味深いものが見つかりました。 –

4
Select [root] = cast((Select * From @t1 for xml raw('FirstTable')) 
        +(Select * From @t2 for xml raw('AnotherTable')) 
       as xml) 
For XML Path(''),Type 

戻り

<root> 
    <FirstTable ID="1" SomeField="rec1" /> 
    <FirstTable ID="2" SomeField="rec2" /> 
    <FirstTable ID="3" SomeField="rec3" /> 
    <FirstTable ID="4" SomeField="rec4" /> 
    <AnotherTable ID="1" SomeField="s106" AnotherField="here" /> 
    <AnotherTable ID="2" SomeField="s12" AnotherField="just" /> 
    <AnotherTable ID="3" SomeField="s13" AnotherField="sample" /> 
</root> 
+0

すごいです!どうもありがとう! –

+0

ok私はカップル分で別のものを送ってきます –

+0

私はここでそれをするつもりですので、新しい質問を投稿することはできません –

関連する問題