DATA_SOURCE USING BULK INSERT in SQL Server does not compile - sql

I'm trying to load data from a sample file, but I'm using couple of parameters in the WITH clause and hence it does not compile.
I'm using SSMS18, Not sure if that is an issue?
Not Working:
BULK INSERT #data
FROM 'sample.csv'
WITH
(
DATA_SOURCE = 'SampleData',
fieldterminator ='|',
rowterminator = '0x0a',
FORMAT = 'CSV'
)
;
Working:
BULK INSERT #data
FROM 'sample.csv'
WITH
(
--DATA_SOURCE = 'SampleData',
fieldterminator ='|',
rowterminator = '0x0a',
--FORMAT = 'CSV'
)
;
If I comment out the DATA_SOURCE then it works but If it has either DATA_SOURCE or FORMAT = 'CSV' then it does not compile
Here is the error from SSMS 18 using Sql server version as below:
Microsoft SQL Server 2016 (SP2-GDR) (KB4583460) - 13.0.5103.6 (X64) Nov 1 2020 00:13:28 Copyright (c) Microsoft Corporation Express Edition (64-bit) on Windows 10 Pro 10.0 (Build 19042: )
Error: Msg 102, Level 15, State 1, Procedure FDB_Load, Line 262 [Batch Start Line 7]
Incorrect syntax near 'FORMAT'.
Can somebody please help?

Related

SQL Server Bulk Insert error - data conversion error (type mismatch or invalid character for the specified codepage)

I'm not a pro in this but I'm trying to do a bulk insert (from csv to SQL Server) but I'm getting some errors:
Msg 4864, Level 16, State 1, Line 1
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 1 (Year).
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)".
Here is the code I have used:
BULK INSERT [dbCen_Staging].dbo.[dc1]
FROM "C:\Newfolder\dc.csv"
WITH
(
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n',
MAXERRORS = 0,
--DATAFILETYPE = 'widechar',
BATCHSIZE=250000,
KEEPIDENTITY
)
GO
So wondering why I'm making a mistake, or if anyone else has a better idea on how to insert a few csv files with 900 millions rows each to SQL Server table. Any on-premise or Azure cloud solution maybe? Because speed matters a lot.
Thanks all

Why can't bulk insert format csv?

I have this code:
BULK INSERT [custdb].[dbo].[TB_T_DISP_PARTY] FROM 'd\DB\dbo.TB_T_DISP_PARTY.csv' WITH ( FIRSTROW = 2, FORMAT = 'CSV' );
And then I get this error
'Msg 102, Level 15, State 1, Line 6
Incorrect syntax near'FORMAT'.'
Can somebody tell me how to fix this error?
It is just bcoz of Version Difference
FORMAT = 'CSV' Applies to: SQL Server 2017 + Versions

SQL Server 2017: IID_IColumnsInfo Bulk Insert Error

I've used the following script in the past without issue, so I'm not sure why it's causing me issues now.
Msg 7301, Level 16, State 2, Line 8
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".
My code:
(
FORMAT = 'CSV',
FIELDQUOTE = '"',
FIRSTROW = 2,
FIELDTERMINATOR = ',', --CSV field delimiter
ROWTERMINATOR = '\n', --Use to shift the control to next row
TABLOCK
)
screenshot of setup and error
File Size: 112 MB
Rows: 322,190
Microsoft Server Management Studio v17.4
Can you try
ROWTERMINATOR = '\r\n'
or
ROWTERMINATOR = '0x0a'
Since you're using a CSV file the row terminator may be a line feed (LF), which 0x0a in the hexadecimal notation for. The example below accounts accounts for this type of row terminator.
BULK INSERT dbo.YourTable
FROM 'C:\FilePath\DataFile.csv'
WITH (
FORMAT = 'CSV',
FIRSTROW = 2,
FIELDQUOTE = '"',
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0x0a',
TABLOCK
);
try removing the FORMAT= 'CSV' line
your file may not be RFC 4180 compliant.
this has worked for me and this error
Make sure there is not a byte-order mark (BOM) at the beginning of the file, which will cause this to fail with this error.

Bulk Insert Not working

I have a Bulk Insert query as follows
BULK INSERT tmp_table FROM 'file.jrl'
WITH (
DATAFILETYPE='widenative' ,
FIELDTERMINATOR = '~' ,
MAXERRORS = 0 ,
ROWS_PER_BATCH = 116064 ,
ROWTERMINATOR = '0x0a' ,
TABLOCK )
It is giving me following error
Msg 4866, Level 16, State 4, Line 1
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 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)".
I am using DATAFILETYPE='widenative' , because my data contains some special characters like Ñ,Ã etc
For RowTerminator value I have also checked with '\n'
My column separator is ~. Is there anything I have to change?
My sample data is as follows
12345 ~asdfdfdfd ~ ~ ~ ~ ~0000000000~ ~0000000000~ ~rrrrñtttttt ~
Do you work for BioWare?
Anyway,
the problem here doesn't come from the row terminator. The error message tells you that the first error occurs at the first column, so there's a problem with your field terminator.
My bet is on the datafiletype. Try 'widechar' instead of 'widenative'. Native field terminator is \t, and using native will make the BULK ignore the FIELDTERMINATOR option.
WKR.

BULK INSERT 4866 and 7301

Trying to BULK import data in SQL server with below lines but getting error:
Msg 4866, Level 16, State 8, Line 3
The bulk load failed. The column is too long in the data file for row 1, column 96. Verify that the field terminator and row terminator are specified correctly.
Msg 7301, Level 16, State 2, Line 3
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".
Is there anything wrong with my statements? As when I use import wizard it works fine.
BULK INSERT BICX.dbo.raw
FROM 'D:\NEW_CDR\NEW.txt'
WITH
(
FIRSTROW = 5,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
As you say the table contains 95 columns, and the error says column 96 is too long you have a problem with your row delimiter.
If your file came from a windows system it most likely is \r\n or you could try 0x0a if that doesn't work
BULK INSERT BICX.dbo.raw
FROM 'D:\NEW_CDR\NEW.txt'
WITH
(
FIRSTROW = 5,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\r\n'
);
or
BULK INSERT BICX.dbo.raw
FROM 'D:\NEW_CDR\NEW.txt'
WITH
(
FIRSTROW = 5,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '0x0a'
);