首先,我对SQL Server 2008中的IF
条件一无所知。
我有一张表[AccountHistory]
wWith 2列AccountType
& Amount
。
AccountType
可以有不同的值(Deposit, Withdrawal, Profit,...etc
),我需要查询才能执行以下操作:
If AccountType = Withdrawal Then Amount = Amount * -1
例如:
AcountType Amount
-----------------------------
Deposit 100
Withdrawal -150
Profit 220
Withdrawal -200
答案 0 :(得分:0)
update AccountHistory
set amount = amount * -1
where accounttype = 'Withdrawal';
答案 1 :(得分:0)
除了John Gibb提供的答案(原则上完全正确)之外,我还会在这里看到你的逻辑 - 是否有任何限制阻止用户在系统环境中输入负值?
如果没有,您可能无意中将负数转为正数,并在数据收集增长时得到一些非常奇怪的结果。此外,此查询只能运行一次 - 一旦存在其他数据,再次执行它会将原始负值转换为正值。
update AccountHistory
set amount = amount * -1
where accounttype = 'Withdrawal'
and amount > 0;