0

This is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Computer Name="WH7" Processior="Intel(R) Core(TM)2 Duo CPU P9600 @ 2.66GHz" 
          Manufacturer="Dell Inc." Model="Latitude E6500" Domain="WORKGROUP" 
          RAM="3536 MB" ServiceTag="DGQKTJ1">
    <BIOS Manufacturer="Dell Inc." Version="Phoenix ROM BIOS PLUS Version 1.10 A19"/>
</Computer>

I want to save root node (i.e <Computer>) attribute values and subnode values (i.e BIOS) into Asset table below.

Create Table Asset
(
   AssetId int Identity(100, 1),
   Name varchar(50),
   Processor varchar(50),
   Manufacturer varchar(100),
   Model varchar(100),
   Domain varchar(100),
   RAM  varchar(100),
   ServiceTag varchar(100),
   Version varchar(100)
)

Anyone please help me.

4

1 回答 1

0

您可以轻松地从节点中提取信息<Computer>并将其插入Asset:(PS:注意:它是一个
处理器-不是“处理器”(两者之后没有is

DECLARE @Input XML

SET @Input = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Computer Name="WH7" Processor="Intel(R) Core(TM)2 Duo CPU P9600 @ 2.66GHz" 
          Manufacturer="Dell Inc." Model="Latitude E6500" Domain="WORKGROUP" 
          RAM="3536 MB" ServiceTag="DGQKTJ1">
    <BIOS Manufacturer="Dell Inc." Version="Phoenix ROM BIOS PLUS Version 1.10 A19"/>
</Computer>'

INSERT INTO dbo.Asset(Name, Processor, Manufacturer, Model, Domain, RAM, ServiceTag)
    SELECT
        Name = @Input.value('(/Computer/@Name)[1]', 'varchar(50)'),
        Processor = @Input.value('(/Computer/@Processor)[1]', 'varchar(50)'),
        Manufacturer = @Input.value('(/Computer/@Manufacturer)[1]', 'varchar(50)'),
        Model = @Input.value('(/Computer/@Model)[1]', 'varchar(50)'),
        Domain = @Input.value('(/Computer/@Domain)[1]', 'varchar(50)'),
        RAM = @Input.value('(/Computer/@RAM)[1]', 'varchar(50)'),
        ServiceTag = @Input.value('(/Computer/@ServiceTag)[1]', 'varchar(50)')

但是完全不清楚的是您希望如何将<BIOS>节点插入此表,以及如何将这两行分开 - 或者它们是否以某种方式链接(这样您就会知道哪个 BIOS 属于哪台计算机...... )

于 2013-09-05T16:16:21.757 回答