将文件夹(OPENROWSET(BULK ...))中的文件插入到包含详细信息的表中

时间:2015-10-18 16:07:07

标签: database file tsql sql-server-2012 directory

我尝试将特定文件夹和子文件夹中的多个文件复制到MS SQL 2012中。我想获取文件详细信息并将其导入数据库中。我想获得以下文件详细信息:

  • 创建日期
  • 修改日期
  • 尺寸

这是我的查询:

DECLARE  @sql nvarchar(max)
        , @filename nvarchar(256)
        , @filepath varchar(100)

DECLARE @FileList TABLE (filename nvarchar(256), depth int, fl int)

INSERT INTO @FileList
EXEC xp_dirtree @filepath, 10, 1;

DECLARE curDoc CURSOR FOR
SELECT filename FROM @FileList WHERE fl = 1

OPEN curDoc FETCH NEXT FROM curDoc INTO @filename

WHILE (@@FETCH_STATUS = 0)
  BEGIN
    SET @sql = 'INSERT INTO _table SELECT * FROM OPENROWSET(BULK ''' + @filepath + @filename + ''', SINGLE_BLOB) as tbl'

    PRINT @sql

    EXEC sp_executesql @sql

    FETCH NEXT FROM curDoc
    INTO @filename
  END

CLOSE curDoc

DEALLOCATE curDoc

0 个答案:

没有答案