将数据从XML导入SQL Server时,在参数中获取空值

时间:2016-10-10 14:47:04

标签: xml sql-server-2008 sqlxml

我有需要传输到SQL Server表的xml文档。一切正常,但不确定为什么我得到参数的空值;在我的例子中是instancekey

XML文档

[{"objectName":"cn=anna,ou=user,ou=system",
"sn":["1"],
"cn":["test1"],
"objectClass":["top","inetOrgPerson","person","organizationalPerson"],
"userPassword":["password"],
"uid":["12345"]},

{"objectName":"cn=tim, ou=user,ou=system",
"sn":["2"],
"cn":["test2"],
"objectClass":["top","inetOrgPerson","person","organizationalPerson"],
"userPassword":["password"],
"uid":["125342"]}
]

用于导入XML数据的SQL Server脚本

console.log(result.data.objectName)

2 个答案:

答案 0 :(得分:1)

FROM OPENXMLsp_xml_preparedocumentsp_xml_removedocument周围调用的使用已过时,应替换为现代XML方法.nodes().query().value().modify()

如果您这样说,您的查询会更快,更清晰,更易于维护:

DECLARE @x XML=
N'<response>
  <item instancekey="0">
    <title>First Steps in Photo Shop and Digital Imaging</title>
    <field_unit_instance_code>VE1EC37</field_unit_instance_code>
    <field_market_area_description>Adults</field_market_area_description>
    <field_ssa_description>Creative Arts, Design and Crafts</field_ssa_description>
    <field_school_owning>Creative Arts and Digital Industries</field_school_owning>
    <field_course_overview />
    <field_entry_requirements />
    <field_teaching_methods />
    <field_modules_and_assessment />
    <field_career_options_and_progres />
    <field_equipment_needed_and_costs />
    <field_work_placement_field_trips />
  </item>
  <item instancekey="1">
    <title>Print Techniques</title>
    <field_unit_instance_code>VE1EC36</field_unit_instance_code>
    <field_market_area_description>Adults</field_market_area_description>
    <field_ssa_description>Creative Arts, Design and Crafts</field_ssa_description>
    <field_school_owning>Creative Arts and Digital Industries</field_school_owning>
    <field_course_overview />
    <field_entry_requirements />
    <field_teaching_methods />
    <field_modules_and_assessment />
    <field_career_options_and_progres />
    <field_equipment_needed_and_costs />
    <field_work_placement_field_trips />
  </item>
</response>';

- 查询

SELECT i.value('@instancekey','int') AS title
      ,i.value('title[1]','nvarchar(300)') AS title
      ,i.value('field_unit_instance_code[1]','nvarchar(300)') AS field_unit_instance_code
      ,i.value('field_market_area_description[1]','nvarchar(3000)') AS field_market_area_description
      ,i.value('field_ssa_description[1]','nvarchar(3000)') AS field_ssa_description
      ,i.value('field_school_owning[1]','nvarchar(3000)') AS field_school_owning
      ,i.value('field_course_overview[1]','nvarchar(3000)') AS field_course_overview
      ,i.value('field_entry_requirements[1]','nvarchar(3000)') AS field_entry_requirements
      ,i.value('field_teaching_methods[1]','nvarchar(3000)') AS field_teaching_methods
      ,i.value('field_modules_and_assessment[1]','nvarchar(3000)') AS field_modules_and_assessment
      ,i.value('field_career_options_and_progres[1]','nvarchar(3000)') AS field_career_options_and_progres
      ,i.value('field_equipment_needed_and_costs[1]','nvarchar(3000)') AS field_equipment_needed_and_costs
      ,i.value('field_work_placement_field_trips[1]','nvarchar(3000)') AS field_work_placement_field_trips
FROM @x.nodes('response/item') AS a(i)

答案 1 :(得分:0)

SELECT *
FROM OPENXML(@hdoc,'/response/item', 2)
WITH(
 CourseInstanceKey int '@instancekey',
 title varchar(300),
 field_unit_instance_code varchar(300),