views:

47

answers:

2

I have a very simple table with only one column (+ the key column). This column is a varchar(N) type. I would like to import data from a text file. The file looks like this:

string1
string2
.
.
.
stringZ

I don't know how to generate a script to insert these values into the table. Or is there an even simplier tool to import in such a scenario?

Update: sorry I forgot that I have another column in this table and its value must be set to the same int for all rows.

Thanks for your help.

+1  A: 

There are several approaches from writing your own little utilty to do it, to using BCP or other built in tools. Probably the easiest approach would be to use the Import Data wizard - in SSMS right click on the database you want to import into, choose Tasks and then Import Data.

keithwarren7
I tried it but I forgot to mention that I have another column in this table and its value must be set to the same int for all rows. So I could not make the import tool work. Of course I could see if I can edit my text file to put this field in each row.
Nicolas Cadilhac
After modifying my input file to add the constant on each row, I was able to use the tool you mentioned (import in SSMS). Thanks.
Nicolas Cadilhac
+2  A: 

You can use bulk insert to do this

BULK
INSERT your_table
FROM 'c:\your_file.txt'
WITH
(
   FIELDTERMINATOR = ',',
   ROWTERMINATOR = '\n'
)
GO
dcp
sorry I forgot that I have another column in this table and its value must be set to the same int for all rows.
Nicolas Cadilhac
Then bulk insert into temp table and select needed values (+ constants) from there into your main table.
Arvo
I chose Keith's solution because it worked without a lot of effort in this case. Maybe your solution would have worked too with more details after I updated my question. So, thanks anyway.
Nicolas Cadilhac