嗨我試圖從XML文件中獲取一些信息到我的MSSQL數據庫,任何人都可以發現錯誤。獲取XML信息到SQL
這裏是我的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 =分鐘(ID),@ MaxID = MAX(ID) FROM 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
,這裏是一個URL到XML我嘗試使用
http://www.metatronics.co.uk/XML/3326091.xml
感謝傢伙,我不斷收到空值,因此它的東西不多IM失蹤,但看着這一切早上我去瘋狂, 約翰之後。
您提供的是要求用戶輸入用戶名和密碼,所以我不能看到該文件的URL。你的表結構是什麼? –
對不起,這裏有一些文件的相關位是在底部 –
ARRRRgh它太長http://www.metatronics.co.uk/XML/3326091.xml這個位置不會要求和憑據我移動它 –