SQL Server 2008 bulk import issue - sql

Ive been working with this for a while and can't find out what I'm doing wrong. I have a CSV file with data such as
123,Jon,Son,M,1
When I run the query
BULK INSERT MYDB2..Dependent FROM 'c:\db3\db.csv'
WITH
(FIELDTERMINATOR=',',
ROWTERMINATOR = '/n')
I get errors like
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 5 (AGE).
The thing is I made EXACT copies of the tables so there is no way my tables can't match.
I believe the problem is my the format of my query.

It does have a little problem, it should be \n, instead of /n
BULK INSERT [Dependent] FROM 'c:\db3\db.csv'
WITH
(FIELDTERMINATOR=',' ,ROWTERMINATOR = '\n')

Related

Can't BULK INSERT Into SQL Table From CSV File [duplicate]

This question already has answers here:
SQL Server - Bulk Insert - FIELDQUOTE does not recognize double quote
(1 answer)
Import CSV file into SQL Server
(14 answers)
Row terminators for text files used for bulk insert
(2 answers)
Closed 8 months ago.
I seem to be unable to run a bulk insert on a SQL table from a CSV file that gets sent through FTP to our server. It runs without error, but alters 0 rows.
If I copy the data into another file, it works, but I need to be able to do this automatically without messing with the file myself. Opening them both, the only differences I can see are that the line breaks are CRLF on the new file, and just LF on the original. Encoding looks to be the same as well on both, so I must be missing something not in the other similar questions asked.
Sample script below:
BULK INSERT dbo.t_process_order_import
FROM 'C:\Root\Product Data\H888 ProcOrd.csv'
WITH
(
FIRSTROW = 2, -- as 1st one is header
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
TABLOCK
)
Sample CSV Data:
ProcessOrder|ProductNumber|MaterialDescription|OrderQuantity|WorkCenter|StartDate|StartTime
001001101111|000000000000101111|TEST|40500.000 |CKET02|20201014|220000
001001221111|000000000000101111|TEST|14124.000 |GHFD02|20210225|032000
You need to use ROWTERMINATOR='0x0a'
Your code will become:
BULK INSERT dbo.t_process_order_import
FROM 'C:\Root\Product Data\H888 ProcOrd.csv'
WITH
(
FIRSTROW = 2, -- as 1st one is header
FIELDTERMINATOR = '|',
ROWTERMINATOR = '0x0a',
TABLOCK
)
As suggested, I try to improve with my source:
https://learn.microsoft.com/en-us/sql/relational-databases/import-export/specify-field-and-row-terminators-sql-server?view=sql-server-ver16
At paragraph "Specifying \n as a Row Terminator for Bulk Import"
Reporting here what is important for the question:
When you specify \n as a row terminator for bulk import, or implicitly use the default row terminator, bcp and the BULK INSERT statement expect a carriage return-line feed combination (CRLF) as the row terminator. If your source file uses a line feed character only (LF) as the row terminator - as is typical in files generated on Unix and Linux computers - use hexadecimal notation to specify the LF row terminator. For example, in a BULK INSERT statement

SQL Bulk insert ignores first data row

I am trying to import a pipeline delimited file into a temporary table using bulk insert (UTF-8 with unix style row terminator), but it keeps ignoring the first data row (the one after the header) and i don't know why.
Adding | to the header row will not help either...
File contents:
SummaryFile_20191017140001.dat|XXXXXXXXXX|FIL-COUNTRY|128
File1_20191011164611.dat|2|4432|2|Imported||
File2_20191011164611.dat|3|4433|1|Imported||
File3_20191011164611.dat|4|4433|2|Imported||
File4_20191011164611.dat|5|4434|1|Imported|INV_ERROR|
File5_20191011164611.dat|6|4434|2|Imported||
File6_20191011164611.dat|7|4434|3|Imported||
The bulk insert throws no error, but it keeps ignoring the first data line (File1_...)
SQL below:
IF OBJECT_ID('tempdb..#mycsv') IS NOT NULL
DROP TABLE #mycsv
create table #mycsv
(
tlr_file_name varchar(150) null,
tlr_record_id int null,
tlr_pre_invoice_number varchar(50) null,
tlr_pre_invoice_line_number varchar(50) null,
tlr_status varchar (30) null,
tlr_error_code varchar(30) null,
tlr_error_message varchar (500) null)
bulk insert #mycsv
from 'D:\TestData\Test.dat'
with (
rowterminator = '0x0A',
fieldTerminator = '|',
firstrow = 2,
ERRORFILE = 'D:\TestData\Import.log')
select * from #mycsv
It's really bugging me, since i don't really know what am i missing.
If i specify FirstRow = 1 th script will throw:
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 2 (tlr_record_id).
Thanks in advance!
"UTF-8 with unix style row terminator" I assume you're using a version of SQL Server that doesn't support UTF-8. From BULK INSERT (Transact-SQL)
** Important ** Versions prior to SQL Server 2016 (13.x) do not support code page 65001 (UTF-8 encoding).
If you are using 2016+, then specify the code page for UTF-8:
BULK INSERT #mycsv
FROM 'D:\TestData\Test.dat'
WITH (ROWTERMINATOR = '0x0A',
FIELDTERMINATOR = '|',
FIRSTROW = 1,
CODEPAGE = '65001',
ERRORFILE = 'D:\TestData\Import.log');
If you aren't using SQL Server 2016+, then you cannot use BULK INSERT to import a UTF-8 file; you will have to use a different code page or use a different tool.
Note, also, that the above document states the below:
The FIRSTROW attribute is not intended to skip column headers. Skipping headers is not supported by the BULK INSERT statement. When skipping rows, the SQL Server Database Engine looks only at the field terminators, and does not validate the data in the fields of skipped rows.
if you are skipping rows, you still need to ensure the row is valid, but it's not for skipping headers. This means you should be using FIRSTROW = 1 and fixing your header row as #sarlacii points out.
Of course, that does not fix the code page problem if you are using an older version of SQL Server; and my point stands that you'll have to use a different technology on 2014 and prior.
To import rows effectively into a SQL database, it is important to make the header formatting match the data rows. Add the missing delimiters, like so, to the header and try the import again:
SummaryFile_20191017140001.dat|XXXXXXXXXX|FIL-COUNTRY|128|||
The number of fields in the header, versus the data fields, must match, else the row is ignored, and the first satisfactory "data" row will be treated as the header.

Bulk insert with Blank values

I have one table with 48 columns in which I want to import data from csv file. My csv file consist of some blank values.
Whenever I uses bulk insert I am getting error:
1) Bulk load data conversion error (type mismatch or invalid character
for the specified codepage) for row 1, column 1 (column name)
2)The OLE DB provider "BULK" for linked server "(null)" reported an
error. The provider did not give any information about the error.
3)Cannot fetch a row from OLE DB provider "BULK" for linked server
"(null)".
I am using sql server 2008
Below is bulk insert command I am using:-
**
bulk insert DataBaseName.dbo.TableName
from 'C:\FolderName\FileName.csv'
with
(
FIRSTROW = 1,
FIELDTERMINATOR =',',
ROWTERMINATOR ='\n',
KEEPNULLS
)**
Please suggest how to handle it..?
For this type of errors make sure the below things:
1.The datalength should be matched according to your .CSV file(use trial and error method and reach your lengths).
The number of columns should be matched(need to check manually).
The datatype conversions should be done implicitly(better to use all nvarchar datatype in order to avoid errors).

BulkInsert CSV file to table SQL Server

I've got some hard problems inserting my CSV file from a location into a table that will be used for making reports and data extraction matched with other data.
Create table #PD_ABC (
Column1
Column2 etc etc
)
BULK INSERT #PD_ABC FROM 'F:\BulkInsert\Andrej\UtkastAntal(23)Export20141003.csv'
WITH (FIELDTERMINATOR = ';',CODEPAGE = 'RAW',ROWTERMINATOR = '0x0a')
insert into Maintenance.dbo.PD_ABC_Del1
select * from #PD_ABC
So far I supose everything should work. I made a similar script for .txt files but when comming to CSV somehow I cannot import them correctly.
This is the erros message I've been receving.
Msg 4863, Level 16, State 1, Procedure PD_ABC_SP, Line 49
Bulk load data conversion error (truncation) for row 1, column 3 (Gldnr).
No idea how to move forward from this.
It looks like your Column3 doesn't have enough characters for data. Is column3 type char or varchar? If so, you should give it more characters.

Msg 102, Level 15, Line 1 , Incorrect syntax (however ...)

I'm running Microsoft SQL Server 2005 and I've got the following query to import the records from my csv.
However it keeps giving me this syntax error
LOAD DATA local INFILE 'C:\Users\Administrator\Downloads\update_05112013.csv' INTO TABLE dbo.Urls
FIELDS TERMINATED BY ';'
ENCLOSED BY '"'
ESCAPED BY '\\'
LINES TERMINATED BY '\n'
Perhaps I'm missing something small , probably ??
Can any of you guys see what I'm doing wrong.
SQL Server BULK INSERT is a good way to insert data in bulk (as the name suggests) but it doesn't actually support CSV files:
http://technet.microsoft.com/en-us/library/ms188609.aspx
Comma-separated value (CSV) files are not supported by SQL Server
bulk-import operations. However, in some cases, a CSV file can be used
as the data file for a bulk import of data into SQL Server.
If you can create a CSV without quotation marks or escaped characters this will work:
BULK INSERT dbo.Urls FROM 'C:\Users\Administrator\Downloads\update_05112013.csv'
WITH
(
FIELDTERMINATOR = ';',
ROWTERMINATOR = '\n'
)