提前感谢。
<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);
答案 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)
。