BatchUpdateException updateCounts return inconsistent results - sql-server

I am trying to find the way to recognise the item that failed in a batch insert using SQL Server (driver: sqljdbc4).
I created a tiny test case that sends a varchar out of range so I got
"java.sql.BatchUpdateException: String or binary data would be truncated."
I cannot understand the value of updateCounts, which is as follows:
When the first batch insert causes error :
=> updateCounts is -3
When the 2nd batch insert causes error :
=> Update counts: -3, 1, 1, 1
When the 3rd batch insert causes error :
=> Update counts: 1, -3, 1, 1
When the 4rd batch insert causes error :
=> Update counts: 1, 1, -3, 1
When the 5th batch insert causes error :
=> Update counts: 1, 1, 1, -3
I am sending 5 items all the time, so I was expecting 5 items in updateCounts.
Does anyone see a pattern here so I can identify the wrong item?

Related

"Bulk load data conversion error (type mismatch or invalid character for the specified codepage)" when trying to Bulk Insert in SQL Server 2019

I am trying to run the following:
BULK INSERT nashvilleHousing
FROM 'C:\Users\Bruce\Downloads\Nashville Housing Data for Data Cleaning (2).csv'
WITH (
FIRSTROW = 2,
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
);
To insert the following CSV data:
I get the following repeating error:
Msg 4864, Level 16, State 1, Line 35
Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 2, column 4 (SalePrice).
This extends downwards to all rows in the original file (so row 3, column 4; row 4, column 4; etc....)
I have tried changing ROWTERMINATOR and FIELDTERMINATOR, and scanning the source data for errors. No luck.

Update SQL Server table using LEFT

I'm trying to update a table in sql server using the below command but I get the following error:
Msg 156, Level 15, State 1, Line 716
Incorrect syntax near the keyword 'LEFT'
Is LEFT not allowed when updating? What can be used instead? Thanks
UPDATE DI.DBO.MHS
SET (LEFT(BATCH_DATE_2, 1) + '0' + RIGHT(BATCH_DATE_2, 6))
WHERE LEFT(BATCH_DATE_2, 1) = 2
You must specify the column that you want to update:
UPDATE DI.DBO.MHS
SET BATCH_DATE_2 = LEFT(BATCH_DATE_2,1) + '0' + RIGHT(BATCH_DATE_2,6)
WHERE LEFT(BATCH_DATE_2,1) = 2
If it is not BATCH_DATE_2 the column that you want to update then use that column after SET.

How to fix error when updating phone number format?

Ok, I am using SQL Server 2016 and I am trying to figure out how to update the phone numbers from the Customers table. Which look like this "1234567890", and when I run this query:
USE ProductsDatabase2;
UPDATE Customers
SET PhoneNumber = '(' + SUBSTRING(PhoneNumber, 1, 3) + ') ' +
SUBSTRING(PhoneNumber, 4, 3) + '-' + SUBSTRING(PhoneNumber, 7, 4)
I get this error message:
Msg 8152, Level 16, State 13, Line 3
String or binary data would be truncated.
The statement has been terminated.
Here is the Customers table:
How do I fix this problem?

How to parse wrong data in a column then save to other table using SQL Server?

How to parse the correct date data then separate or transfer the wrong date data in a separated table.
Example:
Correct Table A
ColumnId, ColumnDate
1, 06/11/2016
2, 簽訂臨時買賣合約後 交易再未有進展 The PASP has not proceeded further
3, 11/11/2016
4, 在 7/12/2016 ,基 於法例35(2)(b)條所 容許的原因,售價 更改為
5, 24-10-2016
Wrong Table B
ColumnId, ColumnDate
2, 簽訂臨時買賣合約後 交易再未有進展 The PASP has not proceeded further
4, 在 7/12/2016 ,基 於法例35(2)(b)條所 容許的原因,售價 更改為

Duplicate identity code in SQL for vb.net winforms

Good day,
I am not too sure about the reason this can happen.
I have a large code in which I insert some values to some SQL tables. The program is working in 4-5 separate machines that access the same SQL data base that is held in another server.
Try : codfact = CInt(Me.CFTableAdapter.codigoQuery) + 1 : Catch ex As Exception : codfact = 1 : End Try
There I get my new id of the table and save it in codfact.
CFTableAdapter.codigoQuery = It is a SELECT that gets the MAX(id) of my table.
And then I do the insert:
While Not exit4
Try
Me.CFTableAdapter.InsertQuery(codfact, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 1, -1, -1, Me.codcom, 0, False, -1, -1, -1, -1, 0, selprop)
exit4 = True
Catch ex As Exception
If ex.Message.Contains("PRIMARY KEY") Then
Try : codfact = CInt(Me.CF.codigoQuery) + 1 : Catch : codfact = 1 : End Try
Else
MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error)
End
End If
End Try
End While
That is the code.
What happens is that sometimes both programs keep working with the same codfact that should be the Unique identity ID in the SQL table.
When I try to reproduce this error I always get the "PRIMARY KEY" error and it gets the next codfact to work with(the correct way).
That codfact is used in other tables as Foreing Key. That way I get a single codfact line instead of 2 or 3.

Resources