将xml插入sql server

时间:2012-07-03 03:12:57

标签: sql-server xml

好的,所以我知道有一些关于将xml导入sql server的其他帖子,但我似乎无法弄明白。我认为我的问题可能与多级别有关。任何人都可以帮忙。我的xml,sql表和我试过的代码。

提前感谢。

<items>
  <item>
    <sku>
      <value>
        the_sku
      </value>
    </sku>
    <short_dis2>
      <value>
        short_discription2
      <value/>
    </short_dis2>
    <short_dis1>
      <value>
        short_discription1
      </value>
    </short_dis1>
    <title>
      <value>
        product_title
      </value>
    </title>
    <detailSpec>
      <value>
        detailed_specification_html
      </value>
    </detailSpec>
    <basicSpec>
      <value>
        basic_overview_html 
      </value>
    </basicSpec>
    <basicSpecHeading>
      <value>
        besic_spec_heading
      </value>
    </basicSpecHeading>
    <detailSpecHeading>
      <value>
        detailed_specifications_heading
      </value>
    </detailSpecHeading>
    <model>
      <value>
        the_model_number
      </value>
    </model>
    <image_file_name>
      <value>
        the_image_url
      </value>
    </image_file_name>
  </item>
  ...

CREATE TABLE [dbo].[products](
    [sku] [nchar](15) NOT NULL,
    [model] [nvarchar](50) NULL,
    [title] [ntext] NULL,
    [short_dis1] [ntext] NULL,
    [short_dis2] [ntext] NULL,
    [basicSpecHeading] [ntext] NULL,
    [basicSpec] [ntext] NULL,
    [detailSpecHeading] [ntext] NULL,
    [detailSpec] [ntext] NULL,
    [image_file_name] [nchar](100) NULL

INSERT INTO products (sku, short_dis2,short_dis1,title,detailSpec,basicSpec,basicSpecHeading,detailSpecHeading,model,image_file_name) 
SELECT X.product.query('sku').value('.', 'nchar(15)'),
       X.product.query('short_dis2').value('.', 'nvarchar(max)'),
       X.product.query('short_dis1').value('.', 'nvarchar(max)'),
       X.product.query('title').value('.', 'nvarchar(max)'),
       X.product.query('detailSpec').value('.', 'nvarchar(max)'),
       X.product.query('basicSpec').value('.', 'nvarchar(max)'),
       X.product.query('basicSpecHeading').value('.', 'nvarchar(max)'),
       X.product.query('detailSpecHeading').value('.', 'nvarchar(max)'),
       X.product.query('model').value('.', 'nvarchar(max)'),
       X.product.query('image_file_name').value('.', 'nvarchar(max)')
FROM ( 
SELECT CAST(x AS XML)
FROM OPENROWSET(
     BULK 'C:\users\me\desktop\xml_sample.xml',
     SINGLE_BLOB) AS T(x)
     ) AS T(x)
CROSS APPLY x.nodes('Products/Product') AS X(product);

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的事情:

DECLARE @Input XML

SELECT @input = CAST(BulkColumn AS XML)
FROM OPENROWSET (BULK 'C:\users\me\desktop\xml_sample.xml', SINGLE_BLOB) AS XMLDATA

INSERT INTO products (sku, short_dis2, short_dis1, title, detailSpec, 
                      basicSpec, basicSpecHeading, detailSpecHeading, model, image_file_name) 
   SELECT
      Item.value('(sku/value)[1]', 'nchar(15)'),
      Item.value('(short_dis2/value)[1]', 'nvarchar(max)'),
      Item.value('(short_dis1/value)[1]', 'nvarchar(max)'),
      Item.value('(title/value)[1]', 'nvarchar(max)'),
      Item.value('(detailSpec/value)[1]', 'nvarchar(max)'),
      Item.value('(basicSpec/value)[1]', 'nvarchar(max)'),
      Item.value('(basicSpecHeading/value)[1]', 'nvarchar(max)'),
      Item.value('(detailSpecHeading/value)[1]', 'nvarchar(max)'),
      Item.value('(model/value)[1]', 'nvarchar(max)'),
      Item.value('(image_file_name/value)[1]', 'nvarchar(max)')
   FROM 
      @input.nodes('/items/item') AS Tbl(Item)

另请注意:NTEXT已弃用 - 您也应在表格定义中使用NVARCHAR(MAX)