简体   繁体   English

SQL 服务器批量插入

[英]SQL Server Bulk Insert

I want to import a one column text file into one of my sql tables.我想将一列文本文件导入我的 sql 表之一。 The file is just a list of swear words.该文件只是一个脏话列表。

I've written the following TSQL to do this我写了以下 TSQL 来做到这一点

BULK INSERT SwearWords
FROM 'c:\swears.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

However it errors with unexapected end of file.但是,它会因意外的文件结尾而出错。 The table im importing to is just an identity field followed by a nvarchar field that I want to insert the text into.我导入到的表只是一个标识字段,后跟一个我想将文本插入到的 nvarchar 字段。 It works fine if I add in the text file "1," to the beginning of eveyr line, I assume this is because SQL if looking for 2 fields.如果我将文本文件“1”添加到每一行的开头,它就可以正常工作,我认为这是因为如果要查找 2 个字段,则为 SQL。 Is there any way around this?有没有办法解决?

Thanks谢谢

You need to use FORMATFILE for this.为此,您需要使用 FORMATFILE。 See BULK INSERT .请参阅批量插入

FORMATFILE [ = 'format_file_path' ] FORMATFILE [ = 'format_file_path' ]

Specifies the full path of a format file.指定格式文件的完整路径。 A format file describes the data file that contains stored responses created using the bcp utility on the same table or view.格式文件描述的数据文件包含使用 bcp 实用程序在同一个表或视图上创建的存储响应。 The format file should be used in cases in which:在以下情况下应使用格式文件:

 * 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.

For more detailed information, see Using Format Files .有关更多详细信息,请参阅 使用格式化文件

This is described in books on line for BULK INSERT under the KEEPIDENTITY argument.这在 KEEPIDENTITY 参数下的 BULK INSERT 在线书籍中有所描述。 Here is what is says这是说的

KEEPIDENTITY Specifies that the values for an identity column are present in the file imported. KEEPIDENTITY 指定标识列的值存在于导入的文件中。 If KEEPIDENTITY is not given, the identity values for this column in the data file imported are ignored, and SQL Server automatically assigns unique values based on the seed and increment values specified during table creation.如果未给出 KEEPIDENTITY,则导入的数据文件中该列的标识值将被忽略,SQL 服务器根据建表时指定的种子值和增量值自动分配唯一值。 If the data file does not contain values for the identity column in the table or view, use a format file to specify that the identity column in the table or view should be skipped when importing data;如果数据文件不包含表或视图中标识列的值,则使用格式文件指定在导入数据时应跳过表或视图中的标识列; SQL Server automatically assigns unique values for the column SQL 服务器自动为列分配唯一值

So, either use a format file or supply a dummy value and make sure not to use the KEEPIDENTITY argument因此,要么使用格式文件,要么提供一个虚拟值,并确保不使用 KEEPIDENTITY 参数

Also, you can create a view on your table based on just the nvarchar column, and then BULK INSERT into your view.此外,您可以仅基于 nvarchar 列在表上创建一个视图,然后将 BULK INSERT 插入到您的视图中。 This is a very clean way of using BULK INSERT.这是使用 BULK INSERT 的一种非常干净的方式。

This way you don't need to worry about your IDENTITY column, or creating a format file.这样您就不必担心您的 IDENTITY 列或创建格式文件。

Your BULK INSERT statement should look like this:您的 BULK INSERT 语句应如下所示:

BULK INSERT vw_SwearWords FROM 'c:\swearwords.txt' WITH (ROWTERMINATOR = '\n')

You need to make sure the structure of your text file and the table match - if the table has two fields, then you will have to provide two fields/columns in the text file as well.您需要确保文本文件的结构与表格匹配 - 如果表格有两个字段,那么您还必须在文本文件中提供两个字段/列。

Since the first column in the SQL table is an IDENTITY field, you can provide any value you want - but you have to have a value there, I don't think there's any way around this.由于 SQL 表中的第一列是一个 IDENTITY 字段,您可以提供任何您想要的值 - 但您必须在那里有一个值,我认为没有任何解决办法。

Marc马克

Check the last line has CR/LF (\r\n).检查最后一行是否有 CR/LF (\r\n)。 Sometimes thats the problem, some other times an extra carriage return is at the end of the file.有时这就是问题所在,有时文件末尾有一个额外的回车符。 You can check that with an hexeditor.您可以使用十六进制编辑器进行检查。

You could remove the identity column and put it back when you're done.您可以删除标识列并在完成后将其放回原位。 Alternatively, if that breaks your database relationships, you could do the import using DTS or SSIS if it's a once off import -- more granular control of fiddling with columns或者,如果这破坏了您的数据库关系,您可以使用 DTS 或 SSIS 进行导入(如果它是一次性导入)——更精细地控制摆弄列

In SQL Server 2008 I've found it easier to just create a file containing a lot of inserts like so, and paste that into Management Studio's query window, rather than fight with getting the format file and bulk insert file permissions just right:在 SQL Server 2008 中,我发现更容易创建一个包含大量插入的文件,并将其粘贴到 Management Studio 的查询 window 中,而不是为获得格式文件和批量插入文件权限而斗争:

USE YourDB
GO
INSERT INTO MyTable (FirstCol, SecondCol)
VALUES ('First',1),
('Second',2),
('Third',3),
('Fourth',4),
('Fifth',5)

http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/ http://blog.sqlauthority.com/2008/07/02/sql-server-2008-insert-multiple-records-using-one-insert-statement-use-of-row-constructor/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM