2010-07-30 2 views
1

같은 구조를 가진 단순한 XML 문서로 가득 찬 폴더가 있으면 모든 문서를 임시 테이블에로드하거나 폴더를 테이블로 처리하는 빠른 방법이 있습니까? SQL 2005 XML 쿼리 구문을 사용하여 문서를 쿼리하려면?SQL 서버의 디렉터리에있는 모든 XML 파일 쿼리

내가 본 대량로드 예제는 모두 XML 문서를로드하는 동안 구문 분석을 시도합니다. 필자가 전체 파일을 'xml'유형의 단일 열로 처리하게되어 기쁘다. 구문 분석은 쿼리 구문에 의해 처리됩니다.

답변

2

모든 XML 파일이 동일한 구조를 가지고 있기 때문에 참으로 좋은 생각입니다. 임시 테이블에 xml 파일을로드하는 것에 대해 잘 모르겠습니다 만 SQL Server 2005의 테이블을 데이터 형식 xml로 유지 관리하고 각 xml 파일을 레코드로 저장할 수 있습니다. XML.Query 또는 XML.Value를 사용하여 쿼리 할 수 ​​있습니다. 이 문서에서는 SQL Server의 XML 쿼리에 대해 알고 싶은 거의 모든 것을 다루고

...

http://www.15seconds.com/issue/050803.htm

BeyondRelational.com
& Jacob Sebastian의 블로그에서 사용할 수있는 풍부한 자습서가있다.

건배!

+0

감사 @Sandy입니다, 첫 번째 링크에 설명 된 'doc ("myxmlfile.xml") ... 기능에 대해 알지 못했습니다. 그게 유용 할거야 ... – grenade

+0

@ 수류탄, 쿨! 하지만, 그것은 하나의 XML 파일에 사용됩니까? – CoderHawk

+0

그것은, 그러나 나는 어떤 종류의 반복을 생각하고있다 ... – grenade

0

해결책은 다음과 같습니다. 나는 루핑 (looping)과 이런 종류의 기능이 포함될 수 있다고 생각할 때 작동하는데 시간이 걸렸다. 어떤 경우에는 아래의 SQL은 각 파일에 대해 하나의 행 사용하여 XML 디렉토리의 내용을 포함하는 테이블 변수와 함께 당신을 떠날 것이다하는 파일 이름을 포함하는 열 및 다른 포함은 XML 내용 :

declare @directory varchar(256) set @directory = 'C:\Temp' 
declare @filecount int, @fileindex int, @linecount int, @lineindex int 
declare @filename varchar(255), @arg varchar(512), @contents varchar(8000) 
set @arg = 'dir ' + @directory + '\ /A-D /B' 
declare @dir table ([filename] varchar(512)) 
insert @dir exec master.dbo.xp_cmdshell @arg 
declare @files table (id int not null identity(1,1), [filename] varchar(512), [content] xml null) 
insert into @files ([filename]) select [filename] from @dir where [filename] like '%.xml' 
select @filecount = count(*) from @files 
set @fileindex = 0 
while @fileindex < @filecount begin 
    set @fileindex = @fileindex + 1 
    select @filename = @directory + '\' + [filename] from @files where id = @fileindex 
    set @contents = '' 
    set @arg = 'type ' + @filename 
    create table #lines(id int not null identity(1,1), line varchar(255)) 
    insert into #lines exec master.dbo.xp_cmdshell @arg 
    select @linecount = count(*) from #lines 
    set @lineindex = 0 
    while @lineindex < @linecount begin 
    set @lineindex = @lineindex + 1 
    select @contents = @contents + line from #lines where Id = @lineindex 
    end 
    drop table #lines 
    update @files set [content] = @contents where id = @fileindex 
end 
select * from @files 
go