Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server 2012 Bulk Insert from CSV into temp table

As the title says, I am attempting to insert a CSV into a temporary table. I am unfortunately encountering errors.

Here is the query:

USE DATABASE5000

CREATE TABLE #tempTable1
(
    ID INT,
    CD VARCHAR(50), 
    ESD DATETIME,
    EED DATETIME, 
    MiscDate DATETIME, 
    SQ SMALLINT
) 

BULK INSERT #tempTable1
FROM 'C:\Dir\Folder\BestFile.csv';

And here are the errors I get:

Msg 4832, Level 16, State 1, Line 1
Bulk load: An unexpected end of file was encountered in the data file.

Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "BULK" for linked server "(null)" reported an error. The provider did not give any information about the error.

Msg 7330, Level 16, State 2, Line 1
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".

Any ideas? Thanks yall.

like image 542
Fishcakes Avatar asked Oct 09 '18 23:10

Fishcakes


People also ask

How to Bulk insert data into a temp table in SQL Server?

You can verify in the tempdb database that the table is created with the data that was stored in the CSV file. In this way, you can bulk insert data into a temp table in SQL Server. You may face some scenarios where you have an identity column in your table. If you try to bulk insert data into that table, new identity values will be assigned.

How to Bulk insert data from CSV files into SQL Server database?

Sometimes there is a scenario when we have to perform bulk insert data from .csv files into SQL Server database. We can use the GUI interface in SSMS (SQL Server Management Studio) to import data from Excel, CSV, etc files.

How to import data from CSV to a new table in SQL?

You can import data from a CSV file into a new table using this Wizard. The SQL Server Import and Export Wizard will create a table and import data into it. In SQL Server Import and Export Wizard, you have to select the data source as Flat File Source and select the CSV file. Also, you have to specify the format of the CSV file at this step.

How to skip the headers in CSV file while importing data?

You can see that all the 15 rows from the CSV file are inserted into the dbo.Persons table. Hence, in this way, you can skip the headers in your CSV file while importing data from this file into a table using the BULK INSERT statement in SQL Server.


1 Answers

You didn't specified any FIELDTERMINATOR. The default value is actually tab. Please refer to BULK INSERT documentation.

BULK INSERT #tempTable1
FROM 'C:\Dir\Folder\BestFile.csv'
WITH
(
    FIELDTERMINATOR = ','           -- add this
);

According to documentation, there is a FORMAT = CSV

WITH (FORMAT = 'CSV')

You may try that. I did a quick test, there are some limitations it seems like does not support string with double quote in it

like image 96
Squirrel Avatar answered Oct 24 '22 20:10

Squirrel