Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error while bulk insertion

I am getting error when I am trying to do bulk insert:

BULK INSERT #tbl_InterCompanyUploadDetail_Staging 
FROM '\\SVRP03546008461D\QA\UploadTemplatewithvalidation.xlsx'
 WITH (FIRSTROW = 6, FIELDTERMINATOR ='\t', ROWTERMINATOR ='\\n' )

Error that I am getting is :

Bulk load data conversion error (truncation) for row 6, column 2 (Oracle Company Code).

The column in Excel has data as 470 and in database column is varchar (10). So what could be the reason for the error.

like image 699
Anirudh Agarwal Avatar asked May 03 '18 10:05

Anirudh Agarwal


People also ask

What happens when bulk insert fails?

When you Bulk Insert the file without specifying or with specifying batch Size , 8 out of 10 get inserted into the table. The Invalid Row's i.e. 8th and 7th gets failed and doesn't get inserted. This Happens because the Default MAXERRORS count is 10 per transaction. Hope this solves the issue.

How does bulk insert work in SQL?

BULK INSERT statement BULK INSERT loads data from a data file into a table. This functionality is similar to that provided by the in option of the bcp command; however, the data file is read by the SQL Server process. For a description of the BULK INSERT syntax, see BULK INSERT (Transact-SQL).

How can increase bulk insert performance in SQL Server?

Below are some good ways to improve BULK INSERT operations : Using TABLOCK as query hint. Dropping Indexes during Bulk Load operation and then once it is completed then recreating them. Changing the Recovery model of database to be BULK_LOGGED during the load operation.


1 Answers

The Issue

BULK INSERT may not work with xlsx files, try converting the .xlsx file to .csv file to achieve this (using BULK INSERT)

1st Solution - using OPENROWSET

Try using OPENROWSET with Microsoft.ACE.OLEDB.12.0 provider:

Insert into <rawdatatable> 
select * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0', 
                         'Excel 12.0;Database=D:\SSIS\FileToLoad.xlsx;HDR=YES', 
                         'SELECT * FROM [Sheet1$]')

OR

SELECT * INTO Data_dq
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0; Database=D:\Desktop\Data.xlsx', [Sheet1$]);

2nd Solution - using OPENDATASOURCE

SELECT * INTO Data_dq
FROM OPENDATASOURCE('Microsoft.ACE.OLEDB.12.0',
'Data Source=D:\Desktop\Data.xlsx;Extended Properties=Excel 12.0')...[Sheet1$];

References

  • Import/Export Excel (.Xlsx) or (.Xls) File into SQL Server
  • Import data from Excel to SQL Server or Azure SQL Database
  • How to Bulk Insert from XLSX file extension?
like image 188
Hadi Avatar answered Sep 24 '22 18:09

Hadi