I have a CSV File with 17 columns, where one column [CustName) has various customer names and some of them have commas.
Example "ABC Company, Inc"
I want to Bulk Insert the CSV file into a table.
The command is:
BULK INSERT [dbo].vPTInsert
FROM '\Server\GROUP-Data\AfftonImports\Invoice_Detail_Export_Report 2-28-20.csv'
WITH ( FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n' ,
FORMATFILE = '\Server\GROUP-Data\AfftonImports\Format.fmt')
I get the error:
Cannot bulk load. Invalid column number in the format file
The format file:
11.0
21
1 SQLCHAR 0 20 "," 1 InvNum QL_Latin1_General_CP1_CI_AS
2 SQLCHAR 0 10 "," 2 InvType QL_Latin1_General_CP1_CI_AS
3 SQLCHAR 0 30 "," 3 BolNum QL_Latin1_General_CP1_CI_AS
4 SQLCHAR 0 40 "," 4 PONum QL_Latin1_General_CP1_CI_AS
5 SQLCHAR 0 11 "\r\n" 5 InvDate QL_Latin1_General_CP1_CI_AS
6 SQLCHAR 0 15 "," 6 BatchNum QL_Latin1_General_CP1_CI_AS
7 SQLCHAR 0 60 "," 7 CustName QL_Latin1_General_CP1_CI_AS
8 SQLCHAR 0 10 "," 8 Company QL_Latin1_General_CP1_CI_AS
9 SQLCHAR 0 10 "," 9 Terminal QL_Latin1_General_CP1_CI_AS
10 SQLCHAR 0 20 "," 10 InvAmt QL_Latin1_General_CP1_CI_AS
11 SQLCHAR 0 11 "\r\n" 11 GLDate QL_Latin1_General_CP1_CI_AS
12 SQLCHAR 0 20 "," 12 Header_Detail QL_Latin1_General_CP1_CI_AS
13 SQLCHAR 0 50 "," 13 ChargeCode QL_Latin1_General_CP1_CI_AS
14 SQLCHAR 0 50 "," 14 GLAccount QL_Latin1_General_CP1_CI_AS
15 SQLCHAR 0 20 "," 15 DebitAmt QL_Latin1_General_CP1_CI_AS
16 SQLCHAR 0 20 "," 16 CreditAmt QL_Latin1_General_CP1_CI_AS
17 SQLCHAR 0 200 "," 17 Memo QL_Latin1_General_CP1_CI_AS
side note: The column headings (Row 1) of the CSV file do not exactly match the column names in the destination table.
The Format File column names are from the the Table, not the CSV file.
So for example Column 7 in the CSV File is labled 'custnumb' but the table is 'CustName'
Not sure that matters given I have FIRSTROW = 2 in the Bulk Insert command.
When I open the CSV file in Textpad 8.0 the data looks like this:
invnum,invtype,bolnum,ponum,invdate,batchnumb,custnumb,company,terminal,invamt,gldate,header_detail,chargecode,glaccount,debitamt,creditamt,memo
918182,Invoice,SGNVH5096400,CHI1874176,2/28/2020,AFT022820,ONE-LINE,1,2201,377,2/4/2020,Header,,1028,377,,TMP# 580327-CONTAINER # TRLU9441196-CHASSIS # FLXZ423713-REF # CHI1874176
918168,Invoice,6226845980,313194183,2/28/2020,AFT022820,"FREIGHT SERVICES, LTD 01",1,2201,314.6,2/21/2020,Header,,1028,314.6,,TMP# 583616-CONTAINER # CSLU6066287-CHASSIS # APMZ400009-REF # 313194183
I notice that the only column enclosed in quotes is the [CustName] (column 7) and then ONLY when the CustName has a Comma in the name.
So I am not sure what I should have for the Delimiters in the Format file, especially for the CustName column given that only some rows have the Double-Quotes.