views:

50

answers:

2

Hi I am having a problem when trying to update a table using an IN clause, I have a big list of clients that should be updated 4500+.

Update table 
set columnA = 'value'
where ID in ( biglistofids )  //biglistofids > 4500 ids

I am getting this error "String or binary data would be truncated."

I tried the same script with fewer ids lets say (2000) and it worked fine.

I have also tried using a temporal table but I got same error.

  1. SELECT Id INTO tmpTable FROM dbo.table WHERE id IN (biglistofids) //create temporal table succesfully
  2. Update table set columnA = 'value' FROM table INNER JOIN tmpTable ON table.ID = tmpTable.ID

Is there any way to handle this, without repeating code for each 2000 records?

Thanks in advance

A: 

It looks to me, based on your error, that the actual problem is with one or more of the values you're updating. I'd try validating the input, first. I've done this many ways based on number of records I had, size of the value, type of value, etc., so that will depend on your specific scenario.

The most straight-forward one (not necessarilly the best) is the one you describe. Try to do 2000. If that works, try the next 2000, etc. That is time intensive and clunky and may not be the best for your situation, but I've never seen it fail to identify my problem.

AllenG
+6  A: 

The "String or binary data would be truncated." has nothing to do with the IN clause.

It means in this line:

set columnA = 'value' 

you are setting columnA to something that is too long to be held in columnA.

Maybe certain ids have corresponding data that is too long, and these are not among the first 2000 you have tried.

GSerg
+1: I reproduced the error on SQL Server 2005, trying to put "abcdef" into an NVARCHAR(5) column.
OMG Ponies
+1 Agree that the message is unrelated to the size of the IN clause
Andomar
That's exactly what was happening, thanks.
Albert