SQL BULK INSERT
First of all you have to make sure that the table structure is identical with the file structure.
You can store the text files path inside a table, loop over these values using a cursor, build the command dynamically then execute the command:
DECLARE @strQuery VARCHAR(4000)
DECLARE @FileName VARCHAR(4000)
DECLARE file_cursor CURSOR
FOR SELECT FilePath FROM FilesTable
OPEN file_cursor
FETCH NEXT FROM file_cursor INTO @FileName;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @strQuery = 'BULK INSERT SchoolsTemp
FROM ''' + @FileName + '''
WITH
(
FIELDTERMINATOR = '','', --Columns delimiter
ROWTERMINATOR = ''\n'', --Rows delimiter
TABLOCK
)
EXEC(@strQuery)
FETCH NEXT FROM file_cursor INTO @FileName;
END
CLOSE file_cursor
DEALLOCATE file_cursor
More information
C# approach: SchemaMapper class library
If you are familiar with C#, recently i started a new project on Github, which is a class library developed using C#. You can use it to import tabular data from excel, word , powerpoint, text, csv, html, json and xml into a unified SQL server table. check it out at:
You can follow this Wiki page for a step-by-step guide: