批量插入文件夹内有许多文件

时间:2014-07-28 16:14:29

标签: sql-server bulkinsert

我想用sql server读取xml文件。我在下面展示了我是如何做到的。

DECLARE @testxml TABLE (IntCol int, XmlCol xml);
    INSERT INTO @testxml(XmlCol)
    SELECT * FROM OPENROWSET(
       BULK 'C:\XMLs\32056963_0001515351.xml',
       SINGLE_BLOB) AS x;

SELECT * FROM @testxml

一切都好。但我需要读取文件夹中的许多文件,所以我正在使用:

EXEC master.sys.xp_dirtree 'C:\XMLs\',0,1;

但是如何进行动态批量插入以将文件夹中的所有xml文件插入@testxml?

1 个答案:

答案 0 :(得分:2)

我不知道是否有某种方法可以同时批量插入所有文件。我建议使用动态查询为每个文件执行导入查询。但是为了能够从主查询中获取数据,您应该将数据插入临时表中,因为表变量的范围将仅限于动态查询。

-- Get the file names
CREATE TABLE #files (
    subdirectory NVARCHAR(255),
    depth INT,
    file BIT
)

INSERT INTO #files
EXEC master.sys.xp_dirtree 'C:\XMLs\',0,1;

-- Iterate through the XML files
DECLARE @filesCursor CURSOR;
SET @filesCursor = CURSOR FOR
SELECT subdirectory
FROM #files
WHERE file=1 AND LEN(subdirectory)>4 AND LOWER(RIGHT(subdirectory,4))='.xml'

DECLARE @fileName NVARCHAR(255), @query NVARCHAR(MAX);

FETCH NEXT FROM @filesCursor INTO @fileName;

-- Temporary table to store the data
CREATE TABLE #testxml (IntCol int, XmlCol xml);

WHILE @@fetch_status = 0
    BEGIN
        -- Build and execute the query for each file
        SET @query = 'INSERT INTO #testxml(XmlCol) SELECT * FROM OPENROWSET(BULK ''C:\XMLs\' + @fileName + ''',SINGLE_BLOB) AS x';
        EXECUTE sp_executesql @query;

        FETCH NEXT FROM @filesCursor INTO @fileName;
    END

-- Closing and deallocating cursor
CLOSE @filesCursor;

DEALLOCATE @filesCursor;

-- Get the data from the temp table into your table variable.
-- If it is not necessary to use a table variable, you could read
-- the data directly from the temp table
DECLARE @testxml TABLE (IntCol int, XmlCol xml);

INSERT INTO @testxml
SELECT * FROM #testxml;

-- Deleting temp tables, as they won't be used anymore
DROP TABLE #testxml;

DROP TABLE #files;