打开和读取文本文件的存储过程
我正在寻找一个存储过程代码,它将打开一个文本文件,读取数千行,并将代码添加到数据库中的表中。有没有一种简单的方法可以在 T-SQL 中实现这一点?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议使用 SSIS。它旨在执行此类操作(特别是如果您需要定期执行此操作)。
这里是一个很好的链接,它介绍了读取文本文件并将其插入数据库。
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.
如果文件已准备好“按原样”加载(无需数据转换或复杂映射),则可以使用批量插入命令:
CREATE PROC dbo.uspImportTextFile
AS
从“C:\ImportFile.txt”批量插入表名
WITH (FIELDTERMINATOR ='|', FIRSTROW = 2)
http://msdn.microsoft.com/en-us/library/ms188365.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms188365.aspx" rel="nofollow">http:// /msdn.microsoft.com/en-us/library/ms188365.aspx
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
将许多记录插入表中的最有效方法是使用 BULK INSERT (我相信这是 BCP 实用程序 使用的,所以它应该一样快)。
BULK INSERT
针对插入大量数据进行了优化,适用于简单INSERT
语句的性能根本无法满足要求的情况。如果
BULK INSERT
不是您想要的,那么您可能需要查看以下文章以获取更简单的技术:文章中链接的是一个存储过程
uftReadFileAsTable
,它似乎应该具有足够的通用性来实现您所追求的目标。如果不是,那么您至少可以使用存储过程作为如何在 SQL 中读取文件的示例(它使用 OLE / Scripting.FileSystemObject)
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 simpleINSERT
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)
为什么不使用尝试用户功能?
这样您就可以使用.NET 来访问和处理您的文件。
看看这个帖子
why don't use try user functions?
This way you can use .NET to access and handle your file.
Check out this post