您好我试图从XML文件中获取一些信息到我的MSSQL数据库中,任何人都可以发现错误。
这是我的sql代码(现在用循环修复,遍历整个目录和错误陷阱)
使用mydb
IF OBJECT_ID('mydb..tempList') IS NOT NULL
DROP TABLE tempList
CREATE TABLE tempList ([FileName] VARCHAR(500))
--plain vanilla dos dir command with /B switch (bare format)
INSERT INTO tempList
EXEC MASTER..XP_CMDSHELL 'dir C:\Data\icecat\xmls /B'
--delete the null values
DELETE tempList WHERE [FileName] IS NULL
-- Delete all the files that don't have xml extension
DELETE tempList WHERE [FileName] NOT LIKE '%.xml'
--this will be used to loop over the table
alter table tempList add id int identity
go
truncate table dbo.XMLImportRelated --in case you want to rerun just this codeblock
DECLARE @Directory varchar(100)
SELECT @Directory = 'C:\Data\icecat\xmls\'
DECLARE @FileExist int
DECLARE @FileName varchar(500),@DeleteCommand varchar(1000),@FullFileName varchar(500), @SQLFullFileName varchar(500)
DECLARE @ProductID int
DECLARE @RelatedID int
- 这样我们就可以知道循环持续多长时间了 DECLARE @LoopID int,@ MaxID int SELECT @LoopID = min(id),@ MaxID = max(ID) 来自tempList
WHILE @LoopID <= @MaxID
BEGIN
SELECT @FileNAme = filename
FROM tempList
WHERE id = @LoopID
SELECT @FullFileName = @Directory + @FileName
EXEC xp_fileexist @FullFileName , @FileExist output
IF @FileExist =1 --sanity check in case some evil person removed the file
BEGIN
DECLARE @x XML, @sql NVARCHAR(MAX);
BEGIN TRY
SELECT @sql = N'SELECT @X = CONVERT(xml,[ICECAT-interface],2)
FROM OPENROWSET(BULK ''' + @FullFileName + ''' ,SINGLE_BLOB)
---------------------^^ escaped quotes are important
AS Import([ICECAT-interface]);';
EXEC sp_executesql @sql, N'@x XML OUTPUT', @x OUTPUT;
END TRY
BEGIN CATCH
PRINT 'Error'
END CATCH
INSERT XMLImportRelated ([ProductID],RelatedID)
select P1.X.value('@ID', 'int') as ProductID,
P2.X.value('@ID', 'int') as RelatedID
from @X.nodes('/ICECAT-interface/Product') as P1(X)
cross apply P1.X.nodes('ProductRelated') as PR(X)
cross apply PR.X.nodes('Product') as P2(X)
-- SET @DeleteCommand = 'del ' + @Directory + @FileName
END
SELECT @LoopID = min(id)
FROM tempList
WHERE id > @LoopID
END
这里是xml我尝试使用
的网址http://www.metatronics.co.uk/XML/3326091.xml
谢谢你们,我一直都是空的,所以它的东西很小,但我看到它整个上午我都疯了, 约翰。
答案 0 :(得分:0)
在您提供的XML中,/ICECAT-interface/Product/ProductRelated
中有四个属性,path
不是其中之一。
尝试:
SELECT
Product.value('@ID', 'int'),
Product.value('@Category_ID','int'),
Product.value('@Reversed','bit'),
Product.value('@Preferred','bit')
FROM ...
如果您想从/ICECAT-interface/Product/ProductRelated/Product
中选择属性,则需要将nodes
子句更改为包含Product
,并指定字段列表中使用的属性。
要从ProductRelated和Product获取值,您应该使用cross apply
。
select PR.X.value('@ID', 'int') as ProductRelatedID,
PR.X.value('@Category_ID','int') as Category,
PR.X.value('@Reversed','bit') as Reversed,
PR.X.value('@Preferred','bit') as Preferred,
P.X.value('@ID', 'int') as ProductID,
P.X.value('@Prod_id', 'nvarchar(500)') as Prod_id,
P.X.value('@ThumbPic', 'nvarchar(500)') as ThumbPic,
P.X.value('@Name', 'nvarchar(500)') as Name
from @X.nodes('/ICECAT-interface/Product/ProductRelated') as PR(X)
cross apply PR.X.nodes('Product') as P(X)
<强>更新强>
select P1.X.value('@ID', 'int') as ProductID,
PR.X.value('@ID', 'int') as ProductRelatedID,
PR.X.value('@Category_ID','int') as Category,
PR.X.value('@Reversed','bit') as Reversed,
PR.X.value('@Preferred','bit') as Preferred,
P2.X.value('@ID', 'int') as ProductID,
P2.X.value('@Prod_id', 'nvarchar(500)') as Prod_id,
P2.X.value('@ThumbPic', 'nvarchar(500)') as ThumbPic,
P2.X.value('@Name', 'nvarchar(500)') as Name
from @X.nodes('/ICECAT-interface/Product') as P1(X)
cross apply P1.X.nodes('ProductRelated') as PR(X)
cross apply PR.X.nodes('Product') as P2(X)
结果:
ProductID ProductRelatedID Category Reversed Preferred ProductID Prod_id ThumbPic Name
----------- ---------------- ----------- -------- --------- ----------- ------------------ ----------------------------------------------- --------------------------------------------------------------
3326091 57050234 258 0 0 1220634 WS-C2960-24-S http://images.icecat.biz/thumbs/1220634.jpg Catalyst 2960-24-S
3326091 68385107 258 0 0 2708530 WS-C2960-48TC-S-RF http://images.icecat.biz/thumbs/2708530.jpg Catalyst 2960-48TC-S
3326091 57050235 258 0 0 1220633 WS-C2960-24TC-S http://images.icecat.biz/thumbs/1220633.jpg Catalyst 2960-24TC-S
3326091 57050236 258 0 0 3328069 WS-C2960-24PC-S http://images.icecat.biz/thumbs/3328069.jpg WS-C2960-24PC-S
3326091 0 182 0 0 16646 DFE-690TXD http://images.icecat.biz/thumbs/16646.jpg 32 Bit Card Bus 10/100Mbps Ethernet Adapter with Direct Port
3326091 57050238 258 0 0 1710023 WS-C2960-48TT-S http://images.icecat.biz/thumbs/1710023.jpg Catalyst 2960-48TT-S
3326091 0 182 0 0 468298 LC102 http://images.icecat.biz/thumbs/468298.jpg LAN PC Card Gigabit
3326091 57050239 258 0 0 1220632 WS-C2960-48TC-S http://images.icecat.biz/thumbs/1220632.jpg Catalyst 2960-48TC-S
3326091 0 883 0 0 1549666 UT012 UTP Cable Cat5E 3M Grey
3326091 57050233 258 0 0 1710024 WS-C2960-8TC-S http://images.icecat.biz/thumbs/1710024.jpg Catalyst 2960-8TC-S
3326091 0 182 0 0 1959688 LC103 http://images2.icecat.biz/thumbs/1959688.jpg Gigabit Network Card PCI Express
3326091 57050237 258 0 0 3326672 WS-C2960-24LC-S http://images.icecat.biz/thumbs/3326672.jpg CATALYST 2960 24 10/100 Port & 8 PoE
3326091 0 244 0 0 1320852 BR-6624 http://images.icecat.biz/thumbs/1320852.jpg BR-6624 Load Balancing Router
3326091 0 182 0 0 489937 LC101 http://images.icecat.biz/thumbs/489937.jpg Gigabit Network Card PCI
3326091 0 182 0 0 72364 DGE-530T http://images.icecat.biz/thumbs/72364.jpg 32-Bit 10/100/1000 Base-T PCI Adapter
3326091 0 1505 0 0 1318217 N-707522 http://images.icecat.biz/thumbs/1318217.jpg UTP Crimping Tang
3326091 0 244 0 0 1684822 20002427 http://images2.icecat.biz/thumbs/1684822.jpg FRITZ!Box Fon WLAN 7270 International Edition
3326091 0 883 0 0 1318218 N-707523 http://images.icecat.biz/thumbs/1318218.jpg UTP CAT5 Pullbox Network Cable, 100m
3326091 0 1172 0 0 1318117 N-707521 http://images.icecat.biz/thumbs/1318117.jpg UTP Shieldcaps for RJ45, 10 Pieces
3326091 0 244 0 0 859910 20002387 http://images.icecat.biz/thumbs/859910.jpg FRITZ!Box Fon WLAN 7170 Annex A
3326091 0 244 0 0 859908 20002388 http://images.icecat.biz/thumbs/859908.jpg FRITZ!Box Fon WLAN 7170 Annex B