SQL Server批量插入

Gav*_*vin 4 sql sql-server bulkinsert

我想将一个列文本文件导入到我的一个sql表中.该文件只是一个发誓单词列表.

我写了以下TSQL来做到这一点

BULK INSERT SwearWords
FROM 'c:\swears.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
Run Code Online (Sandbox Code Playgroud)

但是,它与未预料到的文件结尾有误.导入到的表只是一个标识字段,后跟一个我要插入文本的nvarchar字段.如果我将文本文件"1"添加到eveyr行的开头,它可以正常工作,我认为这是因为SQL查找2个字段.有没有办法解决?

谢谢

Red*_*ter 10

你需要使用FORMATFILE.见BULK INSERT.

FORMATFILE [='format_file_path']

指定格式文件的完整路径.格式文件描述包含在同一表或视图上使用bcp实用程序创建的存储响应的数据文件.格式文件应在以下情况下使用:

* The data file contains greater or fewer columns than the table or view.

* The columns are in a different order.

* The column delimiters vary.

* There are other changes in the data format. Format files are usually created by using the bcp utility and modified with a text editor as needed. For more information, see bcp Utility.
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅使用格式文件.

  • 如何使用不同数量的列插入FORMATFILE的示例?很难找到一个. (2认同)