views:

1140

answers:

6

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

A: 

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

marc_s
isnt this the same exact thing as what the OP wrote??
Stan R.
Essentially, yes--but the OP didn't seem to know why....
RolandTumble
@Stan: I could have sworn at first the post didn't include the "BULK" statement - just "INSERT" - oh well, must have missed it ....
marc_s
A: 

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.

Alan FL
A: 

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

Conrad
A: 

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')
Jesse
+4  A: 

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.

RedFilter
+1 interesting - thanks! I wasn't aware of format file and what they could do for a bulk insert - great tip, thanks!
marc_s
+1  A: 

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

SQLMenace