Вот кое-что, с чего можно начать.Вы можете прочитать о xp_dirtree
и cursors
, чтобы увидеть, как они работают.Если ваши файлы распределены по разным родительским папкам или разным дискам, вам понадобится дополнительный курсор для их получения ...
---------------------------------------------------------------------------------------------------------------
--Set some variables
---------------------------------------------------------------------------------------------------------------
DECLARE @fileLocation VARCHAR(128) = '\\server\e$\data\' --location of files (parent folder)
DECLARE @sql NVARCHAR(4000) --dynamic sql variable
DECLARE @fileName VARCHAR(128) --full file name variable if you want to use this
---------------------------------------------------------------------------------------------------------------
--Get a list of all the file names in the directory
---------------------------------------------------------------------------------------------------------------
IF OBJECT_ID('tempdb..#FileNames') IS NOT NULL DROP TABLE #FileNames
CREATE TABLE #FileNames (
id int IDENTITY(1,1)
,subdirectory nvarchar(512)
,depth int
,isfile bit)
INSERT #FileNames (subdirectory,depth,isfile)
EXEC xp_dirtree @fileLocation, 1, 1
--Here's all the files and folders. Note isFile field.
select * from #FileNames
---------------------------------------------------------------------------------------------------------------
--Create a cursor to fetch the file names
---------------------------------------------------------------------------------------------------------------
DECLARE c CURSOR FOR
select name from #FileNames where isfile = 1
OPEN c
FETCH NEXT FROM c INTO @fileName
---------------------------------------------------------------------------------------------------------------
--For each file, bulk insert to the proper view, update the proper table, update the log, etc...
---------------------------------------------------------------------------------------------------------------
WHILE @@FETCH_STATUS = 0
BEGIN
--do your bulk insert work
FETCH NEXT FROM c INTO @fileName
END