将来自两个不同文件的XML放入单个SQL表

时间:2019-09-04 14:00:17

标签: sql sql-server xml

我有以下查询设置,可以正常使用

WITH XmlFile (xmlData) AS
(
   SELECT CAST(BulkColumn AS XML) 
   FROM OPENROWSET(BULK 'D:\Timewise\results.xml', SINGLE_BLOB) AS x
)

INSERT INTO [dbo].[Timewise] ([Title], [IP])
SELECT c.value('(Title/text())[1]', 'VARCHAR(25)') AS [NameSite]
       , 'video' as [JournalUserName]
       , '' as [JournalPassword]
       , c.value('(IP/text())[1]', 'VARCHAR(25)') AS [IPAddress]
       , '4910' as [Port]
       , 'video' as [VideoUserName]
       , '' as [VideoPassword]
       , c.value('(IP/text())[1]', 'VARCHAR(25)') AS [VideoIP]
       , '4910' as [VideoPort]
       , '1' as [TotalCams]
       , '1' as [TotalVideoWindows]
       , 'true' as [IsRemote]
       , 'false' as [IsDefaultSite]
       ,'' as [hasTwoDifferentUsers]
       , '' as [DVRType]
       , 'false' as [IsRemoteLiveDataRunning]
FROM XmlFile CROSS APPLY xmlData.nodes('DocumentElement/Server_x0020_List') AS t(c);

下一个问题是这些节点中的一些包含来自另一个xml文件的数据,该文件位于D:\ Timewise \ JournalBrowserSiteSettings.xml

我试图对此做一些研究,以得到一个例子,而在我覆盖了WITH语句的情况下:

WITH XmlFile (xmlData) AS
(
   SELECT CAST(BulkColumn AS XML) 
   FROM OPENROWSET(BULK 'D:\Timewise\results.xml', SINGLE_BLOB) AS x
)
, 
 XmlFile2 (xmlData) AS
(
   SELECT CAST(BulkColumn AS XML) 
   FROM OPENROWSET(BULK 'D:\Timewise\JournalBrowserSiteSettings.xml', SINGLE_BLOB) AS y
)

老实说,我不知道如何从另一个文件中提取此数据(即TotalCams,TotalVideoWIndows,IsRemote,hasTwoDIfferentUsers和DVRType),现在将近2个小时的搜索使我无从下手。 >

1 个答案:

答案 0 :(得分:0)

似乎您知道如何将XML放入SQL表中。

如何将XML文件放入两个不同的表中,然后基于一个公共键(例如,标题?

那行得通。不是吗?