我尝试将特定文件夹和子文件夹中的多个文件复制到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