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