Stored Procedure to Open and Read a text file

橙三吉。 提交于 2019-12-01 04:43:39

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

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.

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)

why don't use try user functions? This way you can use .NET to access and handle your file.

Check out this post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!