Sergey Metlov
Sergey Metlov

Reputation: 26341

SQL conditional update query

I have:
Table with columns A int, B int, C int

I need to build query like:

UPDATE
    Table
SET
    A -= B -- and then if A < 0 do A = C  

Is it possible to do without cursor? If it is useful, I use MS SQL Server 2008.

Upvotes: 2

Views: 1971

Answers (1)

Conrad Frix
Conrad Frix

Reputation: 52675

If I understood correctly this is what you're looking for

UPDATE
    Table
SET
    A = CASE 
           WHEN (A - B) < 0 THEN
             C
           ELSE 
               (A - B)
         END

Upvotes: 8

Related Questions