I have a folder with 4000+ pipe delimited files of which are named the same as tables in a schema.
I'm trying to do this
DECLARE @tableName varchar(max)
DECLARE @sqlCommand varchar(max)
DECLARE @path varchar(20)
SET @path = 'x:\xfer\'
DECLARE tNames CURSOR
FOR
SELECT table_name FROM information_schema.tables
WHERE TABLE_TYPE='BASE TABLE'
AND TABLE_NAME = 'sales_time_pd'
ORDER BY TABLE_NAME
OPEN tNames
FETCH FROM tNames
INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sqlCommand = 'BULK INSERT @tableName
FROM @path + @tableName
WITH
(
FIELDTERMINATOR ='' |'',
ROWTERMINATOR ='' |\n''
);'
EXEC(@sqlCommand)
FETCH NEXT FROM tNames
into @tableName
END
CLOSE tNames;
DEALLOCATE tNames;
I am looking for the concept roughly please excuse my incorrect use of apostrophes
As a test I am targeting a single file/table to do the bulk insert. I have learned that table names need to be static.
So how can I bulk insert into a collection of tables from a directory of files with a corresponding name?