sqlsql-serverstored-proceduresstored-functions

Stored Procedure to Open and Read a text file


I am looking for a stored procedure code that will open a text file, read in several thousand lines, and add the code to a table in the database. Is there a simple way to implement this in T-SQL?


Solution

  • If the file is ready to load "as-is" (no data transformations or complex mappings required), you can use the Bulk Insert command:

    CREATE PROC dbo.uspImportTextFile

    AS

    BULK INSERT Tablename FROM 'C:\ImportFile.txt' WITH ( FIELDTERMINATOR ='|', FIRSTROW = 2 )

    http://msdn.microsoft.com/en-us/library/ms188365.aspx