Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to use case in update statements

Author  Topic 

smithani
Starting Member

42 Posts

Posted - 2007-06-05 : 15:19:46
Hi,
I have this update statement that works, it updates the totalamount to calc amount, but I want to update totalamount only when it is not equal to calcamt.I have tried many things but in vain.Can some one please help me.
How do i use case statements to update only when totalamount!=calcamt.

update c
set totalamount= calcamt
from Prepay c
JOIN
( select sum(amt) as calcamt, payid
from pay
group by payid
)b ON b.payid= c.payid
where c.cust_no='somenum'

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-06-05 : 15:21:41
update c
set totalamount = case when totalamount <> calcamt then calcamt else totalamount end
from ....


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

smithani
Starting Member

42 Posts

Posted - 2007-06-05 : 15:55:45
Thank you thank you so much.I was trying != instead of <>, could that have been my problem and also I had nothing in my else just else end.
thanks a bunch
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2007-06-05 : 15:57:05
!= should work too...

_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-06 : 09:49:50
quote:
Originally posted by smithani

Thank you thank you so much.I was trying != instead of <>, could that have been my problem and also I had nothing in my else just else end.
thanks a bunch

You should have posted your case when ... query

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -