嗨,我希望有人能帮助我,我正在尝试以XML格式将XML元素导入到SQL表中。
首先,我有一个名为Chassis.xml的XML文件,它看起来像这样。
<Chassis> <Chassis Id="1" Chassis="blah blah" Suitability="1" Structured="1" /> <Chassis Id="2" Chassis="blah blah" Suitability="1" Structured="1" /> <Chassis Id="3" Chassis="Blah Blah" Suitability="1" Structured="1" /> <Chassis Id="4" Chassis="Blah Blah" Suitability="1" Structured="1" /> </Chassis>
我正在尝试编写一个将元素导入表的SQL过程,这是我想要的表布局。
test.hardwareComponents
Id TypeId XmlData ---------------------------------- 1 0001 <Chassis Id="1" Chassis="blah blah" Suitability="1" Structured="1" /> 2 0001 <Chassis Id="2" Chassis="blah blah" Suitability="1" Structured="1" />
TypeId将是一个外键,它将在以后定义另一个表中的Type,因此TypeId 0001是机箱ComponentType。
我尝试的每件事都不断失败,我花了数小时尝试这样做,但我感到很沮丧,任何人都可以帮助我。
你尝试过类似的东西吗
DECLARE @xml XML SET @xml = '<Chassis> <Chassis Id="1" Chassis="blah blah" Suitability="1" Structured="1" /> <Chassis Id="2" Chassis="blah blah" Suitability="1" Structured="1" /> <Chassis Id="3" Chassis="Blah Blah" Suitability="1" Structured="1" /> <Chassis Id="4" Chassis="Blah Blah" Suitability="1" Structured="1" /> </Chassis>' SELECT T2.Loc.value('@Id', 'INT') ID, T2.Loc.query('.') FROM @xml.nodes('/Chassis/Chassis') as T2(Loc)