sql server update 与case 用法

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 19:37   599   0


update 计算日期 set 当前年=case when(计算日期>substring(计算日期,1,4)+'0301') then
substring(计算日期,1,4) else 计算日期-1 end

///////////////////////////网络文章///////////////////////////////

如果更新的时候需要对2个条件都要判断的可以用一个update +case来完成
例子如下:
declare @a table(id int,EndTime datetime,classid int)
insert @a
select 1,dateadd(day,-1,getdate()),1
union all
select 2,dateadd(day,1,getdate()),2
union all
select 3,dateadd(day,-1,getdate()),2
union all
select 4,dateadd(day,1,getdate()),1
select * from @a
select getdate()
update @a
set EndTime=dateadd(hh,case when Classid=1 then 24 else 72 end,casewhen EndTime>=getdate() then Endtime elsegetdate()end)
select * from @a

(4 行受影响)
id EndTime classid
----------- ----------------------- -----------
1 2008-07-24 11:52:53.827 1
2 2008-07-26 11:52:53.827 2
3 2008-07-24 11:52:53.827 2
4 2008-07-26 11:52:53.827 1

(4 行受影响)


-----------------------
2008-07-25 11:52:53.827

(1 行受影响)

(4 行受影响)

id EndTime classid
----------- ----------------------- -----------
1 2008-07-26 11:52:53.827 1
2 2008-07-29 11:52:53.827 2
3 2008-07-28 11:52:53.827 2
4 2008-07-27 11:52:53.827 1

(4 行受影响)
/////////////////////另一篇文章////////////////

UPDATE CASE WHEN (2010-05-1713:54:27)转载▼
标签: 杂谈 分类: Mysql
其实是为了实现一个很小的功能,就是为了同步两张表,
如果需要被同步的表中有值的话,则不做修改,如果没值(为空),则同步过来。
SQL语句如下:
1)
UPDATE `test` SET
`content_id` =
CASE WHEN `content_id` IS NULL THEN `content_id`
ELSE `content_id` + 1
END

2)
UPDATE `test` SET
`content_id` =
CASE ISNULL(`content_id`) WHEN 1THEN `content_id`
ELSE `content_id` + 1
END


附上ISNULL函数用法:
ISNULL(expr)
如expr 为NULL,那么ISNULL() 的返回值为 1,否则返回值为 0。
mysql> SELECT ISNULL(1+1);
-> 0
mysql> SELECT ISNULL(1/0);
-> 1
使用= 的NULL 值对比通常是错误的。
/////////////////另一篇文章////////////

2011-03-04 10:28

update case

ALTER proc usp_clearActiveScoreCalcCommentScore
as
begin
update tbForum set UltimoScore=
(case
whenAllScore-RemainScore<0 then 0
elseAllScore-RemainScore
end),AllScore=
(case
whenActivityDegree>=0 andActivityDegree<=300 then 250
whenActivityDegree>=301 andActivityDegree<=800 then 500
whenActivityDegree>=801 andActivityDegree<=2000 then 1000
whenActivityDegree>=2001 andActivityDegree<=4000 then 2000
whenActivityDegree>=4001 andActivityDegree<=8000 then 4500
whenActivityDegree>8000 then9000
end),UltimoDegree=ActivityDegree,ActivityDegree = 0
update tbForum set RemainScore=AllScore
end

-------------------------------

其它用法:

--★★★★★★★★★★SQL里面的case不能象switch那样来用,很遗憾!!★★★★★★★★★★★★★

--简单case
--[dbo.TBL_EVENT]的Status字段检测 生成一个eventStatus列
select EventName,
case [Status]
when 0 then 'ing'
when 2 then 'over'
else '-'
end as eventStatus
from TBL_EVENT

--布尔case
--在布尔case中,如果遇到了第一个为真的when条件,就不会再继续对其他的when条件进行检测
select
case
when 1<0 then '1怎么会小于0撒'
when datepart(yy,getdate())='2006' then '今天是我的生日'
when 1>0 then '1>0当然是正确的啊'
end as result

--布尔case表达式可以处理包含and or 的复杂条件
declare @b int,@q int

set @b=2007
set @q=25

select
case
when @b=2007 and @q between 10 and 30 then 1
else null
end as test

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP