BULK INSERT with inconsistent number of columns

前端 未结 5 1510
名媛妹妹
名媛妹妹 2020-12-10 07:21

I am trying to load a large amount data in SQL server from a flat file using BULK INSERT. However, my file has varying number of columns, for instance the first row contains

5条回答
  •  青春惊慌失措
    2020-12-10 08:20

    My workaround (tested in T-SQL):

    1. Create table with colum count = minimum column count of your import file
    2. Run bulk insert (it will succeed now)

    In last table column, you will find all rest items (including your item separator)

    If it is necessery for you, create another full-columned table, copy all columns from first table, and do some parsing only over last column.

    Example file

    alpha , beta , gamma
    one   , two  , three , four
    

    will look like this in your table:

    c1      | c2     | c3
    "alpha" | "beta" | "gamma"
    "one"   | "two"  | "three , four"
    

提交回复
热议问题