update 怎么用if else 选择执行两者之一的语句呀?

chenyong2010320 2010-12-19 01:45:49
CREATE VIEW dbo.C
AS
SELECT emp_id,charge_money,card_balance,op_date,1 as u
from mchargerecords
union
select emp_id,card_consume,card_balance,sign_time ,0 as u
from Mealrecords



update employee set last_balance=
if(c.u=0)
ISNULL((select top 1 charge_money from c where emp_id=employee.emp_id order by op_date),0)+

ISNULL((select top 1 card_balance from c where emp_id=employee.emp_id order by op_date),0)from c where employee.emp_id ='060359'

else
ISNULL((select top 1 card_balance from c where emp_id=employee.emp_id order by op_date),0)-

ISNULL((select top 1 charge_money from c where emp_id=employee.emp_id order by op_date),0) from c where employee.emp_id ='060359'
...全文
876 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenyong2010320 2010-12-19
  • 打赏
  • 举报
回复
谢谢 太感谢了……
billpu 2010-12-19
  • 打赏
  • 举报
回复
case when then else end
bancxc 2010-12-19
  • 打赏
  • 举报
回复
case when
「已注销」 2010-12-19
  • 打赏
  • 举报
回复
sample:

UPDATE b
SET bno = CASE id WHEN 1 THEN 'ddd' ELSE 'eee' END
FROM book b

id Bno Bclass Bchubanshe Bzuozhe
----------- ---- ------------ ------------------------------ --------------------
1 ddd f d f00
2 eee a a a00
3 eee f df f00
4 eee sdf asdf sdf00
7 eee sdf fdsa sdf00
12 eee df a df00

(6 row(s) affected)
「已注销」 2010-12-19
  • 打赏
  • 举报
回复
update employee set last_balance=
case c.u when 0 then
ISNULL((select top 1 charge_money from c where emp_id=employee.emp_id order by op_date),0)+

ISNULL((select top 1 card_balance from c where emp_id=employee.emp_id order by op_date),0)
from c where employee.emp_id ='060359'

else
ISNULL((select top 1 card_balance from c where emp_id=employee.emp_id order by op_date),0)-

ISNULL((select top 1 charge_money from c where emp_id=employee.emp_id order by op_date),0) end
from c where employee.emp_id ='060359'
xiaoxiangqing 2010-12-19
  • 打赏
  • 举报
回复
只能用case when来判断就行了
飘零一叶 2010-12-19
  • 打赏
  • 举报
回复
case when

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧