question

David 2 1 avatar image
David 2 1 asked

Using bcp Or Bulk Insert To Insert Data From A CSV File With 2 Identical Columns?

Hi, I'm new to SQLServer and wondering how the below may be possible: I have a csv file that contains the following columns: CLIENT, NOTES, NOTES. As you can see there are 2 NOTES columns both of which need to be imported automatically using bcp or Bulk Insert into the table DATA_LOAD which has the following columns CLIENTNO, NOTES_FIELD1, NOTES_FIELD2. How can I use bcp or Bulk Insert to successfully load all 3 columns into their appropriate places in the table? Thanks in advance.
t-sqlsql-server-2000bcpbulk-insert
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Kev Riley avatar image
Kev Riley answered
There are too many options/questions to give a complete answer here, so I would suggest looking at the BOL entries for both [`BULK INSERT`][1] and [`bcp`][2]. As long as the import file data types match the table data types, and are in the same order you shouldn't have too many issues. However some guidance: - Using BULK INSERT will be something along the lines of `BULK INSERT DATALOAD FROM 'c:\dataloadfile.csv'` - It sounds like you have column headers in your data - the `FIRSTROW` option might sound good, but as stated in BOL >The FIRSTROW attribute is not intended to skip column headers. Skipping headers is not supported by the BULK INSERT statement. When skipping rows, the SQL Server Database Engine looks only at the field terminators, and does not validate the data in the fields of skipped rows. - using bcp will be like `bcp DATALOAD in dataloadfile.csv -T -c` - where -T is used to specify Windows authentication, and -c specifies character data Another option is [`OPENROWSET`][3] [1]: http://msdn.microsoft.com/en-us/library/ms188365.aspx [2]: http://msdn.microsoft.com/en-us/library/ms162802.aspx [3]: http://msdn.microsoft.com/en-us/library/ms175915.aspx
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 2 1 avatar image David 2 1 commented ·
Thanks all. Got it loading.
0 Likes 0 ·
Leo avatar image
Leo answered
You can use SSIS if you are using SQL 2005 and above. Or DTS in SQL 2000. You might need to change in SSIS Columns for one of NOTES to NOTES as NOTES1 or something.....
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David 2 1 avatar image David 2 1 commented ·
I am using SQLServer 2000 and am setting up a job in DTS using EXEC MASTER.DBO.xp_cmdshell 'bcp.... However I need to script the bcp or bulk insert part to accept more than 1 NOTES column (with I suppose a format file). This is what I really want to know. Thanks
0 Likes 0 ·
Fatherjack avatar image
Fatherjack answered

You could use LogParser, it will automatically rename the second Notes column to Notes2 or you could concatenate it to the 1st one as part of the import process. Some details here http://www.simple-talk.com/community/blogs/jonathanallen/archive/category/1043.aspx

10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Leo avatar image
Leo answered
You can done it by using following way ...... You can remove Dynamics Task (Pass Parameter) if you are using the same file name. Otherwise use the Global Variable and pass the parameters by using Dynamics Task. You can set the Column Name in Text File (Source) and Transfer to Database. After you set those up, you can run the Job by calling something like 'DTSRun /S "(local)" /N "Test Package" /G "{9EA5A37A-40F6-4F35-B901-68A2E627C6B0}" /A "Global_var1":"22"="24" /A "Global_var2":"8"="TEST" /W "0" /E' (Or) Set up as the schedule job - [Microsoft DTS Link][1] ![CSV File Import][2] [1]: http://support.microsoft.com/kb/269074 [2]: /upfiles/DTS_2000_Package_Designer.jpg
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.