Question

I want to import a one column text file into one of my sql tables. The file is just a list of swear words.

I've written the following TSQL to do this

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. 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. Is there any way around this?

Thanks

Was it helpful?

Solution

You need to use FORMATFILE for this. See BULK INSERT.

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

OTHER TIPS

This is described in books on line for BULK INSERT under the KEEPIDENTITY argument. Here is what is says

KEEPIDENTITY Specifies that the values for an identity column are present in the file imported. 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. 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

So, either use a format file or supply a dummy value and make sure not to use the KEEPIDENTITY argument

Also, you can create a view on your table based on just the nvarchar column, and then BULK INSERT into your view. This is a very clean way of using BULK INSERT.

This way you don't need to worry about your IDENTITY column, or creating a format file.

Your BULK INSERT statement should look like this:

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.

Marc

Check the last line has 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

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:

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top