如何将一个表中的所有记录作为XML数据类型提取到第二个表的另一列

时间:2018-05-15 09:07:59

标签: sql sql-server

我想从一个表中获取所有记录作为XML数据类型,并将其存储在第二个表的另一列中。

我有两列将匹配这两个表进行加入,报告和交换。

我是否必须使用ID逻辑或者请帮我解释这个逻辑。

示例:

DECLARE @VAL XML
    SET @VAL =  (   
 SELECT 
                     [ID]                           AS [ID]                                 
                    ,[EventType]                    AS [EventType]                          
                    ,[ClientMsgID]                  AS [ClientMsgID]                            
                    ,[SessionID]                    AS [SessionID]                          
                    ,[Protocol]                     AS [Protocol]   
                    FROM TableA O
                    JOIN Tableb T
                    ON O.[ReportID] = T.ReportID 
                    where o.[exchange] = t.exchange
                    FOR XML  PATH ('row'))

insert into TableC
select ID,record_no,@VAL,null,getdate()
from TableA     

1 个答案:

答案 0 :(得分:0)

您没有提供有关桌面设计的足够详细信息。

如果我理解正确,您希望从另一个表中使用XML更新表格,为此您可以尝试以下内容。

UPDATE ST
SET ColumnToStoreXml=(select  * from FristTable FT 
                             where FT.ColumnToMatch=ST.ClumnToMatch for xml PATH ('row') )
FROM SecondTable ST

修改:问题更新后。

如果要插入新表,请尝试以下操作。

INSERT INTO tablec 
SELECT id, 
       record_no, 
       (SELECT [id]          AS [ID], 
               [eventtype]   AS [EventType], 
               [clientmsgid] AS [ClientMsgID], 
               [sessionid]   AS [SessionID], 
               [protocol]    AS [Protocol] 
        FROM   tablea O 
               JOIN tableb T 
                 ON O.[reportid] = T.reportid 
        WHERE  o.[exchange] = t.exchange 
               AND O.id = T1.id 
        FOR xml path ('row')), 
       Getdate() 
FROM   tablea T1 

注意:在插入中使用列名称总是一个好习惯,例如

insert into tablec(col1,col2,col3)
select col1,col2,col3 from sometable