SQL Server : why does Bulk Load fail? - sql

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'
)

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 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.
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

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 bulk insert error while inserting .txt file

I'm using SQL Server 2012 and when using bulk insert, the following error occurs:
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)".
My query is:
BULK INSERT _bulk
FROM 'D:\Twilight\Personal\Alexander\result.txt'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
The data contained in my .txt file is:
1,alex,trichy
2,arun,namakkal
3,shiva,chennai
Most likely your rows are terminated by \r\n instead of \n.
You can check this in text editors like Notepad++ or Sublime.
If the command below works, the line-endings were the issue:
BULK INSERT _bulk
FROM 'D:\Twilight\Personal\Alexander\result.txt'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\r\n')

sql server - bulk insert error

I am using bulk insert and getting below error:
Note: The data in the load file is not beyong the configured column length
Running Command:
bulk insert load_data from 'C:\temp\dataload\load_file.txt' with (firstrow = 1, fieldterminator = '0x09', rowterminator = '\n',MAXERRORS = 0, ERRORFILE = 'C:\temp\dataload\load_file')
Contents of load file:
user_name file_path asset_owner city import_date
admin C:\ admin toronto 04/12/2012
Error:
Msg 4863, Level 16, State 1, Line 1
Bulk load data conversion error (truncation) for row 1, column 6 (validated).
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)".
The number of columns was incorrect. I had recently changed the table schema but forgotten to do a refresh on the table.
I solved the same problem by changing the data type in the schema. I had date type changed to nvarchar... It worked