Share via

SQL Buckcopy

2956 1 Reputation point
2022-07-13T01:46:35.003+00:00

I have a DB table with 10 fields. I have a .csv file containing 6 fields of the table. How can use bulk copy to insert those records into the DB? The file only has 6 of 10 fields that the DB table contains. Thanks.

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.


4 answers

Sort by: Most helpful
  1. Erland Sommarskog 134.7K Reputation points MVP Volunteer Moderator
    2022-07-13T21:07:44.56+00:00

    You can set up a format file for the field mapping. Say that your CSV files has data for columns 1, 3, 4, 5, 7 and 10:

       9.0  
       6  
       1 SQLVARCHAR  0 0 "," 1 col1 ""  
       2 SQLVARCHAR  0 0 "," 3 col3 ""  
       3 SQLVARCHAR  0 0 "," 4 col4 ""  
       4 SQLVARCHAR  0 0 "," 5 col5 ""  
       5 SQLVARCHAR  0 0 "," 7 col6 ""  
       6 SQLVARCHAR  0 0 "\r\n" 10 col10 ""  
    

    The command would be:

       BULK INSERT MyTable FROM 'C:\temp\MyCSVFile.txt'  
       WITH (FORMATFILE = 'C:\temp\MyCSVFile.fmt')  
    

    Was this answer helpful?

    0 comments No comments

  2. Yitzhak Khabinsky 27,196 Reputation points
    2022-07-13T12:50:41.84+00:00

    Hi @2956 ,

    Another approach would be to create a view with six columns on top of the destination table.
    The view structure will match the *.csv file with six data elements per line.

    Bulk insert will use that view to load data from the *.csv file.
    Behind the scenes, the actual table will be populated.

    BULK INSERT dbo.vMyView  
    FROM  'e:\Temp\input_file.csv'  
    ...  
    

    Was this answer helpful?

    0 comments No comments

  3. Olaf Helper 47,621 Reputation points
    2022-07-13T05:17:49.22+00:00

    Use a stagging table matching the CSV content, bulk copy the CSV to it and then copy over the content to the final table.

    Was this answer helpful?

    0 comments No comments

  4. Bert Zhou-msft 3,521 Reputation points
    2022-07-13T02:37:57.863+00:00

    Hi,@2956

    csv is imported into a database table, usually using bulk insert, but you must know that using it requires the same matching fields, the typical approach is to load the data into a temporary table, then move it to the base table, modify it as appropriate, Refer this link and link2.

    Bert Zhou

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.