Stored Procedure To Open And Read A Text File
Solution 1:
I would recommend looking at using SSIS. It's designed to do this sort of thing (especially if you need to do it on a regular basis).
Here is a good link that goes over reading a text file and inserting into the DB.
Solution 2:
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 )
Solution 3:
The most efficient way of inserting many records into a table is to use BULK INSERT (I believe that this is what the BCP Utility uses, and so it should be just as fast).
BULK INSERT
is optimised for inserting large quantities of data and is intended to be used when the performance of a simple INSERT
statement simply won't do.
If BULK INSERT
isn't what you are after then you might want to take a look at the following article for a more straightforward technique:
Linked in the article is a stored procedure uftReadFileAsTable
which seems like it should be versatile enough to achieve what you are after.
If it isn't then you can at least use the stored procedure as an example of how to read files in SQL (it uses OLE / the Scripting.FileSystemObject)
Solution 4:
why don't use try user functions? This way you can use .NET to access and handle your file.
Check out this post
Post a Comment for "Stored Procedure To Open And Read A Text File"