Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Chrome Orange
Starting Member
6 Posts |
Posted - 2005-11-11 : 17:07:15
|
Hi allis this possibleI want to use BULK INSERT to import a csv with a similar format to1,1,"my name is"there is an unknown number of columns so the table is created by reading the first line of the csv and then I bulk insert into the table. the problem with the csv as above is that, using FIELDTERMINATOR=',' I get "my name is" inserted into the 3rd column, including the "" which is a problemI could use a replace function to get rid of the "" but is takes a very long time on some of the csv files - last one was 280,000 lines.any ideas??TIA |
|
nr
SQLTeam MVY
12543 Posts |
|
Chrome Orange
Starting Member
6 Posts |
Posted - 2005-11-13 : 10:44:30
|
so basically you are saying use a 'formatfile' to sort out the "" as its loaded?So can I create a format file on the fly? |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-11-13 : 13:40:32
|
Yep - see http://www.nigelrivett.net/FTP/s_ftp_PutFile.htmlfor creating a text file.==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
Chrome Orange
Starting Member
6 Posts |
Posted - 2005-11-14 : 19:22:33
|
thank you for that. I've just finished beating my head against a wall for not thinking of that before! |
 |
|
Chrome Orange
Starting Member
6 Posts |
Posted - 2005-11-15 : 09:30:08
|
okI've ended up with a csv that looks like "FIELD1","FIELD2","FIELD3","FIELD4","FIELD5","FIELD6"1,1,"data","data","data","data"2,1,"data","data","data","data"3,1,"data","data","data","data"a fmt file that looks like8.061 SQLCHAR 0 60 "," 1 FIELD1 SQL_Latin1_General_Cp1_CI_AS2 SQLCHAR 0 60 ","" 2 FIELD2 SQL_Latin1_General_Cp1_CI_AS3 SQLCHAR 0 60 "","" 3 FIELD3 SQL_Latin1_General_Cp1_CI_AS4 SQLCHAR 0 60 "","" 4 FIELD4 SQL_Latin1_General_Cp1_CI_AS5 SQLCHAR 0 60 "","" 5 FIELD5 SQL_Latin1_General_Cp1_CI_AS6 SQLCHAR 0 60 ""\n" 6 FIELD6 SQL_Latin1_General_Cp1_CI_ASand I'm gettingInvalid destination table column number for source column 2 in format fileany pointers greatefully received |
 |
|
nr
SQLTeam MVY
12543 Posts |
Posted - 2005-11-15 : 10:22:56
|
You have to escape the double quotes.2 SQLCHAR 0 60 ",\"" 2 FIELD2 SQL_Latin1_General_Cp1_CI_AShave a look at the last example ofhttp://www.mindsdoor.net/SQLTsql/BCP_quoted_CSV_Format_file.html==========================================Cursors are useful if you don't know sql.DTS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
Chrome Orange
Starting Member
6 Posts |
Posted - 2005-11-15 : 10:35:22
|
perfect! thanks |
 |
|
|
|
|
|
|