简体   繁体   中英

UPDATE - CASE Statement - Way too many results

I'm trying to do a very simple data insert on a table using a case statement, see below:

begin tran
  update tblworkitems
  set fkidsupplier = (
    case
    when idworkitem = 214159 then 50290
    when idworkitem = 270370 then 50367
    when idworkitem = 225533 then 50453
    ...
    end )
where 
fkidtemplateitem = 654 and
fkidsupplier is null

I have ~85 of these explicit values. idWorkItem is a primary key, fkidSupplier is a foreign key (Hungarian notation much?). Problem is ~350 row are being returned. I've done this with similar things a million times before and have never had this.

A quick check confirms no duplicates (schema should enforce this anyway):

select idworkitem 
from tblworkitems 
where fkidtemplateitem = 654
group by idworkitem having count(*) > 1

Yes, I understand the WHERE clause is invalid with these primary keys.

Any ideas?

With the structure you have, you still update all the records that meet the criteria in WHERE clause. For those that are not specified in CASE , it will simply set NULL .

What you should do is to specify all records with idworkitem that you want to update in WHERE clause.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM