Import fixed width text to SQL

前端 未结 5 1201
梦谈多话
梦谈多话 2020-12-15 14:50

We have records in this format:

99 0882300 25 YATES ANTHONY V MAY 01 12 04 123456 12345678

The width is fixed and we need to import it int

5条回答
  •  悲哀的现实
    2020-12-15 15:14

    question is pretty old but might still be relevant.

    I had exactly the same problem as you. My solution was to use BULK INSERT, together with a FORMAT file.

    This would allow you to:

    1. keep the code much leaner
    2. have the mapping for the text file to upload in a separate file that you can easy tweak
    3. skip columns if you fancy

    To cut to the chase, here is my data format (that is one line)

    608054000500SS001 ST00BP0000276AC024   19980530G10379    00048134501283404051N02912WAC 0024 04527N05580WAC 0024 1998062520011228E04ST 04856      -94.769323       26.954832     
    -94.761114       26.953626G10379    183    1
    

    And here is my SQL code:

    BULK INSERT dbo.TARGET_TABLE
       FROM 'file_to_upload.dat' 
       WITH (
                BATCHSIZE = 2000,
                FIRSTROW = 1,
                DATAFILETYPE = 'char',
                ROWTERMINATOR = '\r\n',
                FORMATFILE = 'formatfile.Fmt'
    
            );
    

    Please note the ROWTERMINATOR parameter set there, and the DATAFILETYPE.

    And here is the format file

    11.0
    6
    1   SQLCHAR 0   12  ""  1   WELL_API    SQL_Latin1_General_CP1_CI_AS
    2   SQLCHAR 0   19  ""  2   SPACER1     SQL_Latin1_General_CP1_CI_AS
    3   SQLCHAR 0   8   ""  3   FIELD_CODE  SQL_Latin1_General_CP1_CI_AS
    4   SQLCHAR 0   95  ""  4   SPACER2     SQL_Latin1_General_CP1_CI_AS
    5   SQLCHAR 0   5   ""  5   WATER_DEPTH SQL_Latin1_General_CP1_CI_AS
    6   SQLCHAR 0   93  ""  6   SPACER3     SQL_Latin1_General_CP1_CI_AS
    

    I put documentation links below, but what you must note is the following:

    1. the ""s in the 5th column, which indicates the separator (for a .csv would be obviously ","), which in our case is set to just "";
    2. column 2 is fully "SQLCHAR", as it's a text file. This must stay so even if the destination field in the data table is for example an integer (it is my case)

    Bonus note: in my case I only needed three fields, so the stuff in the middle I just called "spacer", and in my format file gets ignored (you change numbers in column 6, see documentation).

    Hope it answers your needs, works fine for me. Cheers

    Documentation here: https://msdn.microsoft.com/en-us/library/ms178129.aspx https://msdn.microsoft.com/en-us/library/ms187908.aspx

提交回复
热议问题