Search code examples
sql-serverbcp

BULK INSERT from file which has extra values


How can I tell the format file that the column in csv file should be ignored. I tried putting 0s and I get invalid column number error:

Format file: 

10.0
9
0      SQLCHAR             0       12      "\t"     1     ID                   ""
2      SQLCHAR             0       10      "\t"     2     Symbol               SQL_Latin1_General_CP1_CI_AS
0      SQLCHAR             0       11      "\t"     3     DateDone             ""
0      SQLCHAR             0       19      "\t"     4     TimeDone             ""
4      SQLCHAR             0       10      "\t"     5     Side                 SQL_Latin1_General_CP1_CI_AS
5      SQLCHAR             0       12      "\t"     6     Size                 ""
6      SQLCHAR             0       41      "\t"     7     Price                ""
7      SQLCHAR             0       10      "\t"     8     Exchange             SQL_Latin1_General_CP1_CI_AS
8      SQLCHAR             0       12      "\r\n"   9     Position             ""

Sample row of csv data

------------------------------------------------------------------------------------------------------------------------
|AccountName || ExecSymbol ||   ExecDateTime || ExecSide    || ExecSize || ExecPrice    || ExecExchange || PositionSize|
 ------        ------------  ----------------  ------------  -----------   ----------      -------------   -------------
    PRIMU$    ||   SCO      ||    1/2/2013    ||   B         ||  100     ||  38.87       ||   ARCA       ||   100

Solution

  • The easiest way is to create an 'fmt' file that can specify what you want to import and what you to ignore:

    https://msdn.microsoft.com/en-us/library/ms179250.aspx