Berenice
Berenice

Reputation: 43

Multiple UPDATE, same column, different values, different WHERE conditions

I am trying to update a field with different conditions in where. Actually I have the next instructions:

Update StmHistItm Set SHI_nPayable = @minPayable where SHI_iItemPK = @minrecno
Update StmHistItm Set SHI_nPayable = @maxPayable where SHI_iItemPK = @maxrecno

Is there a way to do this two instruction in just one?

Upvotes: 2

Views: 15199

Answers (1)

Mureinik
Mureinik

Reputation: 311393

You could use a case expression and combine the where clauses:

UPDATE StmHistItm 
SET    SHI_nPayable = CASE SHI_iItemPK 
                      WHEN @minrecno THEN @minPayable
                      WHEN @maxrecno THEN @maxPayable
                      END
WHERE  SHI_iItemPK IN (@minrecno, @maxrecno)

Upvotes: 4

Related Questions