我有一个简单的更新声明。
Update tbABC
Set salary = 1000, name = 'mike'
Where Id = 1
我需要在更新工资时添加条件,如果工资= 0,则更改为1000,否则工资不会改变。
我做了我的研究,发现了一个类似的问题。 using a conditional update statement in sql
Update tbABC
Set salary = CASE WHEN (salary = 0) then 1000 ELSE ??????? END,
name = 'mike'
Where Id = 1
我被困在那????部分。现在确定要把它放在那里使它成为薪水=工资。
答案 0 :(得分:2)
这应该有效
Update tbABC
Set salary = CASE WHEN (salary = 0) then 1000 ELSE salary END,
name = 'mike'
Where Id = 1
答案 1 :(得分:2)
除非绝对必要,否则我可能更喜欢使用WHERE子句而不是复杂的CASE函数。简化,这将给出:
update tbABC set salary=1000, name='mike' -- using condition for both field updates
where Id=1 and salary=0;
或保留交易的确切逻辑:
update tbABC set salary=1000 -- by ID & only if second condition met
where Id=1 and salary=0;
update tbABC set name='mike' -- by ID.
where Id=1;
我真的不相信有无条件更新员工姓名的现实案例,但他的薪资更新有一些条件。