SQL Server 2012 Bulk Insert from CSV into temp table - sql

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.

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

Related

Script to Import data into SQL table from flat file (text file) .XYZ file

I am trying to create a script to import flat files into SQL Server tables. I tried using the import wizard but since I need to do this periodically I would have to create a SQL function in order to achieve this and I am not sure how to go about it. The flat files are stored in the following format:
19350.000 45978.000 1560.631
19352.000 45978.000 1560.234
19354.000 45978.000 1560.021
19356.000 45978.000 1559.809
19358.000 45978.000 1559.596
I have tried the following:
CREATE TABLE #TempTable
(
Id int identity (1,1),
X float,
Y float,
Z float
)
BULK INSERT #TempTable FROM
'\\fcgwnt01\share.$\StandardHaulage\TEST\Automated\EVO\SurfaceFiles\EVO 2019-
01-23.xyz'
WITH (FIELDTERMINATOR = '**\t**', ROWTERMINATOR = '\n')
SELECT * INTO [dbo].[SHM_EVO_SURFACE_DETAILS] FROM #TempTable
--Drop temporary table
DROP TABLE #TempTable
But I'm getting the following errors
Msg 4866, Level 16, State 1, Line 12
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified correctly.
Msg 7399, Level 16, State 1, Line 12
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 12
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)"

SQL Server : why does Bulk Load fail?

Bulk Load fails when I uncomment the rownum line with the following errors. I know the workaround for this issue. But I need to understand why does it show error message.
Msg 4866, Level 16, State 1, Line 41
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator and row terminator are specified correctly.
Msg 7399, Level 16, State 1, Line 41
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 41
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Code:
CREATE TABLE #TEMPFILE
(
LINE VARCHAR(5000)
,rownum int identity(1,1) primary key
)
EXEC('BULK INSERT #TEMPFILE FROM '''+ #FILENAME + ''' WITH (ROWTERMINATOR = ''0x0a'', lastrow = 1) ')
This is the syntax i have used for bulk insert in SQL Server
BULK
INSERT Table_Name
FROM FileName/FilePath
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)

SQL import/create columns in SMILE chemical structures

We try to import a csv file where the first column includes chemical structures (SMILE) like this
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)NCC(=O)N(C4)C,14-BENZODIAZEPINEDERIV.4_145,1
c1cccc(c12)n(C)c(c2)CN(C)C(=O)c(c3)ccc(c34)N[C#H](C(=O)N(C4)C)CC(=O)OC,14-BENZODIAZEPINEDERIV.3_146,1
Here is the code in SQL
--Define Table
CREATE TABLE Amide_actives_test
(Structure VARCHAR(40),
Name VARCHAR(40),
Active INT)
GO
--Import Data from CSV
BULK
INSERT Amide_actives_test
FROM 'C:\Amide_actives.csv'
WITH
(
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n' --Use to shift the control to next row
)
GO
--Check the content of the table
SELECT * FROM Amide_actives_test
GO
The following error message will pop out:
Bulk load data conversion error (truncation) for row 1, column 1 (Name).
Msg 4863, Level 16, State 1, Line 10
...repeating the previous 2 lines 10 times....
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 10
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Apparently there is a problem of SQL to read the first column in "Structure VARCHAR(40)". I have tried all the string types (CHAR,VARCHAR.NCHAR,NVARCHAR,NTEXT,TEXT) and none of them works.
http://msdn.microsoft.com/en-us/library/ff848814.aspx
There is one way to solve this issue is to purchase another customized MySQL module from DayLight. However, 1. it costs 2. it doesn't support SQL
http://www.daylight.com/dayhtml/doc/pgsql/daycart_pg_search.html
May I know if any SQL guru has SQL solutions? Thanks!
First problem is Structure VARCHAR(40) varchar length is lesser than the input so you got trucation error. Try increasing the varchar length and check

SQL Server Insert Statement From a CSV File

I'm trying to follow the example on this page:
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
The database is on another machine and I'm connecting to it using SQL Server Management Studio 2012.
Here's the insert statement
BULK
INSERT CSVTest
FROM '\\BPWKS\network share\csvtest.csv'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
GO
When I go to check the content of the table, I get the following error:
Error: Msg 208, Level 16, State 82, Line 1
Invalid object name 'CSVTest'.
Table Definition
Create Table CSVTest
(
ID INT,
FirstName VARCHAR(40),
LastName VARCHAR(40),
BirthDate SMALLDATETIME
)
GO
I've also tried importing the local file at 'C:\csvtest.csv'
Resolution appears to be
Missing Table initially
Missing file or permissions to access
file.

SQL BULK INSERT FROM errors

I'm attempting to insert a CSV file into an Microsoft SQL Server Management Studio database like this:
BULK INSERT [dbo].[STUDY]
FROM 'C:\Documents and Settings\Adam\My Documents\SQL Server Management Studio\Projects\StudyTable.csv'
WITH
(
MAXERRORS = 0,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)
But I am getting errors:
Msg 4863, Level 16, State 1, Line 2
Bulk load data conversion error (truncation) for row 1, column 9 (STATUS).
Msg 7399, Level 16, State 1, Line 2
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 2
Cannot fetch a row from OLE DB provider "BULK" for linked server "(null)".
Unfortunately, I cannot provide the contents of StudyTable.csv, to protect the privacy of the company we're working for.
EDIT I can vouch for the validity of the csv file though. It was exported from OpenOffice's version of Excel, and I went through and made sure it was valid.
EDIT2
Here's a dummy version of the CSV file:
1234,,,1234,1234,,"asdf","asdf","Z","asd",7/1/2010 12:23,8/5/2010 13:36,9/4/2010 13:36,"(asdf,1661,#1234,F,T)","F",,,"F",,"68866",1234,1234,1234,"F"
Here's a create script for the STUDY table:
CREATE TABLE [dbo].[STUDY]
(
[STUDY_ID] INT IDENTITY(1,1) NOT NULL,
[PARENT_ID] INT,
[GROUP_ID] INT,
[WORKFLOW_NODE_ID] INT,
[STUDY_TEMPLATE_ID] INT,
[INSPECTION_PLAN_ID] INT,
[NAME] VARCHAR(255),
[DESCRIPTION] VARCHAR(4000),
[STATUS] VARCHAR,
[OLD_STATUS] VARCHAR,
[CREATED_ON] DATE,
[COMPLETED_ON] DATE,
[AUTHORIZED_ON] DATE,
[EVENTS] VARCHAR,
[NEEDS_REVIEW] CHAR,
[HAS_NOTES] CHAR,
[HAS_AUDITS] CHAR,
[STUDY_PART] CHAR,
[STUDY_TYPE] VARCHAR,
[EXTERNAL_REFERENCE] VARCHAR,
[CREATED_BY] INT,
[COMPLETED_BY] INT,
[AUTHORISED_BY] INT,
[ARCHIVED_CHILD_COMPLETE] CHAR
)
It sounds like the data in your STATUS column in the .csv file is longer than the definition for the field in your SQL Table.
Check the definition on that field and make sure the data you have in the .csv will fit (both length and type).
The first error is likely because a column is too big to fit the database. First thought is does the CSV file contain column headers?
The second error is very generic.
Do any of your fields contain a comma as part of their data - for example 37, High Street which isn't encased in quotes? I've come unstuck lots of times in the past because of "issues" in the data (the data is valid, just not quite what's expected). If your CSV file is a few thousand rows or more - what happens if you try importing just the first hundred (or ten) rows?