Wednesday, March 28, 2012

import multiple XML files at once

Hi,

I have about 300-400 XML files I want to load in my SQL database (2005). The following code will load one (1) file. How do i do a mulitple collections?

INSERT INTO MEL (DATA)SELECT *FROM OPENROWSET (BULK
'C:\Temp\CHAPTER1.xml', SINGLE_BLOB)AS TEMP

Thanks,

http://www.sqlservercentral.com/columnists/smoore/importingxmlfilesintosqlserver.asp

See this article.. i think ur schema should be same for each file...

|||

Thank you for the help. This script runs in VBS, how do I do this in VB.NET or from with SQL (stored procedure)? Also I am looking to import the XML as RAW XML. My XML files are large and complex and I want to store them in a table with TYPE of the field "XML".

Thanks Again,

Bones

|||

Example to use the below stored proc

-- Listing 2

CREATE TABLE #Files (MyFile varchar(200))

CREATE TABLE #Lines (MyLine varchar(8000))

DECLARE @.MyFile varchar(200), @.SQL varchar(2000), @.Path varchar(400)

SET @.Path = 'C:\Program Files\Microsoft SQL Server\MSSQL\'

EXECUTE sp_ListFiles @.Path,'#Files','%.txt',NULL,0

SELECT @.MyFile = MyFile FROM #Files WHERE MyFile LIKE 'README%'

SET @.SQL = 'BULK INSERT #Lines FROM ' + CHAR(39) + @.Path + @.MyFile + CHAR(39)

EXECUTE (@.SQL)

SELECT * FROM #Lines

DROP TABLE #Files

DROP TABLE #Lines

1---------------------23StoredProcedure:sp_ListFiles45---------------------6789USE master10GO11CREATE PROCEDURE dbo.sp_ListFiles12 @.PCWritevarchar(2000),13 @.DBTablevarchar(100)=NULL,14 @.PCIntravarchar(100)=NULL,15 @.PCExtravarchar(100)=NULL,16 @.DBUltrabit = 017AS1819SET NOCOUNT ON2021DECLARE @.Return int22DECLARE @.Retainint23DECLARE @.Statusint2425SET @.Status = 02627DECLARE @.Taskvarchar(2000)2829DECLARE @.Work varchar(2000)3031DECLARE @.Wishvarchar(2000)3233SET @.Work ='DIR ' +'"' + @.PCWrite +'"'3435CREATE TABLE #DBAZ (Name varchar(400),Work int IDENTITY(1,1))3637INSERT #DBAZEXECUTE @.Return = master.dbo.xp_cmdshell @.Work3839SET @.Retain =@.@.ERROR4041IF @.Status = 0SET @.Status = @.Retain42IF @.Status = 0SET @.Status = @.Return4344IF (SELECTCOUNT(*)FROM #DBAZ) < 44546BEGIN4748 SELECT @.Wish =Name FROM #DBAZWHERE Work = 14950IF @.WishISNULL5152BEGIN5354 RAISERROR ('General error [%d]',16,1,@.Status)5556END5758 ELSE5960 BEGIN6162 RAISERROR (@.Wish,16,1)6364END6566 END6768ELSE6970 BEGIN7172 DELETE #DBAZWHEREISDATE(SUBSTRING(Name,1,10)) = 0ORSUBSTRING(Name,40,1) ='.'ORNameLIKE'%.lnk'7374IF @.DBTableISNULL7576BEGIN7778 SELECTSUBSTRING(Name,40,100)AS Files79FROM #DBAZ80WHERE 0 = 081AND (@.DBUltra = 0ORNameLIKE'%<DIR>%')82AND (@.DBUltra != 0ORNameNOT LIKE'%<DIR>%')83AND (@.PCIntraISNULL ORSUBSTRING(Name,40,100)LIKE @.PCIntra)84AND (@.PCExtraISNULL ORSUBSTRING(Name,40,100)NOT LIKE @.PCExtra)85ORDER BY 18687END8889 ELSE9091 BEGIN9293 SET @.Task =' INSERT ' +REPLACE(@.DBTable,CHAR(32),CHAR(95))94 +' SELECT SUBSTRING(Name,40,100) AS Files'95 +' FROM #DBAZ'96 +' WHERE 0 = 0'97 +CASEWHEN @.DBUltra = 0THEN''ELSE' AND Name LIKE ' +CHAR(39) +'%<DIR>%' +CHAR(39)END98 +CASEWHEN @.DBUltra != 0THEN''ELSE' AND Name NOT LIKE ' +CHAR(39) +'%<DIR>%' +CHAR(39)END99 +CASEWHEN @.PCIntraISNULLTHEN''ELSE' AND SUBSTRING(Name,40,100) LIKE ' +CHAR(39) + @.PCIntra +CHAR(39)END100 +CASEWHEN @.PCExtraISNULLTHEN''ELSE' AND SUBSTRING(Name,40,100) NOT LIKE ' +CHAR(39) + @.PCExtra +CHAR(39)END101 +' ORDER BY 1'102103IF @.Status = 0EXECUTE (@.Task)SET @.Return =@.@.ERROR104105IF @.Status = 0SET @.Status = @.Return106107 END108109 END110111DROP TABLE #DBAZ112113SET NOCOUNT OFF114115RETURN (@.Status)116117GO118119-- Listing 2120

The above proc will return the list of files in a folder. Use the resulSet of this proc and then run a a cursor or loop to execute your procedure

|||

Thanks Satya

You've been very helpful.

Bones

No comments:

Post a Comment