我正在尝试设置 SQL 作业以将 XML 文件导入 SQL Server 表。使用 OPENXML,我似乎无法从文件中选择我需要的特定数据。这是我的代码和 XML 数据。我正在尝试选择 Facility 和 Entity_Code 但是当我运行代码时,这些字段显示为空白。
我想将这些字段转移到他们自己的表中。
提前致谢。
Declare @x xml
select @x=p
from OPENROWSET(Bulk'\\vmirsdh01\fast_data\Small.xml', SINGLE_BLOB) as T(P)
Select @x
Declare @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
Select *
FROM OPENXML (@hdoc,'/Report/Tablix1/Details_Collection/Details',0)
with(Facility nvarchar(255) '@Facility',
Entity_Code nvarchar(255) '@Entity_Code')
exec sp_xml_removedocument @hdoc
'************ XML
<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="T-Report https://csre.xxx.com%2FDevelopment%20Folder%2FIand%2FT-Report&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=4keav12uayp33ve3uczpgmfr&rc %3ASchema=True" Name="T-Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="T_Report">
<Tablix1>
<Details_Collection><Details Facility="Fxx" Tool_Type="Base Build" Entity_Code="EquiP1" /></Details_Collection>
</Tablix1>
</Report>
这是一个可执行版本
Declare @x xml
select @x='<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="T-Report https://csre.xxx.com%2FDevelopment%20Folder%2FIand%2FT-Report&rs%3ACommand=Render&rs%3AFormat=XML&rs%3ASessionID=4keav12uayp33ve3uczpgmfr&rc %3ASchema=True" Name="T-Report" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="T_Report">
<Tablix1>
<Details_Collection><Details Facility="Fxx" Tool_Type="Base Build" Entity_Code="EquiP1" /></Details_Collection>
</Tablix1>
</Report>'
Declare @hdoc int
EXEC sp_xml_preparedocument @hdoc OUTPUT, @x
Select *
FROM OPENXML (@hdoc,'/Report/Tablix1/Details_Collection/Details',0)
with(Facility nvarchar(255) '@Facility',
Entity_Code nvarchar(255) '@Entity_Code')
exec sp_xml_removedocument @hdoc