Import CSV into SQL Server (bulk insert method)

Information 1 :

The following is a sample CSV with a title:

Name,Class,Subject,ExamDate,Mark,Description
Prabhat,4,Math,2/10/2013,25,Test data for prabhat.
Murari,5,Science,2/11/2013,24,"Test data for his's test, where we can test 2nd ROW, Test."
sanjay,4,Science,,25,Test Only.

And the SQL statement to be imported:

BULK INSERT SchoolsTemp
FROM 'C:\CSVData\Schools.csv'
WITH
(
    FIRSTROW = 2,
    FIELDTERMINATOR = ',',  --CSV field delimiter
    ROWTERMINATOR = '\n',   --Use to shift the control to next row
    TABLOCK
)

References

https://cloud.tencent.com/developer/ask/48590

------------------------------------------------------------------------------

Information 2 :

Sometimes we may import the data in CSV into a database table, for example, when doing report analysis.
For this problem, I think it is not difficult for programmers! But if SQL Server can accomplish this task, would n’t it be better!
Yes, SQL Server does have this feature.
First let's take a look at the CSV file, which is saved in my D: drive, named csv.txt, and the content is: Now it is the key part of SQL Server;
csv-pic-1  

We are using the SQL Server BULK INSERT command. For a detailed explanation of this command, please click here ;
we first create a data table in SQL Server to save this information,
CREATE  TABLE  CSVTable (
Name  NVARCHAR ( MAX ) ,
Email  NVARCHAR ( MAX ),
Area NVARCHAR (MAX)
)
and then execute the following statement:

BULK INSERT CSVTable
FROM 'D:\csv.txt'
WITH(
	FIELDTERMINATOR = ',',
	ROWTERMINATOR = '\n'
)
SELECT * FROM CSVTable
Press F5, the execution result is as follows:
  

how about it? Is it simpler than using a program!

But now there are a few issues to consider:

1. Some column values ​​in the CSV file use double quotes, and some column values ​​do not have double quotes: if you run the above statement again, the result is different from the previous result:some of the columns contain double quotes, This should not be the result we want. To solve this problem, we can only use the temporary table, first import the CSV into the temporary table, and then remove the double quotes during the process of importing from this temporary table to the final table. 2. The column values ​​of the CSV file are all composed of double quotation marks: This problem is slightly more complicated than the previous one. In addition to importing the CSV file into the temporary table, you must also modify the CSV file into the temporary table. Code:Pay attention to the part in the circle. 3. The columns of the CSV file are more than the columns of the data table : and our data table has only three columns. If the above import code is executed, what will be the results? The result is:it puts all the following in the Area column. To deal with this problem, it is actually very simple. That is, we put the column values ​​we want in the data table in order to create a column, and put the unnecessary ones. The column value is also created in the data table, but it is only a temporary column. When importing this data table to the final table, ignore the temporary column.
csv-pic-3 

csv-pic-4 


csv-pic-5 

csv-pic-6


csv-pic-7 


csv-pic-8

Reference materials:

https://www.cnblogs.com/newstar0101/archive/2010/03/15/1685886.html

Published 69 original articles · Like 72 · Visit 240,000+

Guess you like

Origin blog.csdn.net/londa/article/details/101532371