UPDATE中的CASE确定要更新的列?

时间:2015-09-02 17:18:59

标签: sql sql-server tsql

如何使用会影响哪一列更新的CASE?

UPDATE TABLE1
    CASE WHEN [Status] = 'Alpha' THEN AlphaStatus = Status

使用解决方案here,我尝试了以下内容,但它只更新了1列。

DECLARE @Table TABLE
(
    ID int,
    AlphaStatus varchar(10),
    BetaStatus varchar(10),
    GammaStatus varchar(10)
)

insert into @table (id) select 1
insert into @table (id) select 2


Declare @Values Table
(
 ID varchar(20),
 [Group] varchar(20),
 [Status] varchar(20)
)

insert into @Values ( ID, [Group], [Status]) select 1, 'Alpha', 'ENABLED'
insert into @Values ( ID, [Group], [Status]) select 1, 'Beta', 'ENABLED'
insert into @Values ( ID, [Group], [Status]) select 1, 'Gamma', 'DISABLED'
insert into @Values ( ID, [Group], [Status]) select 2, 'Alpha', 'ENABLED'
insert into @Values ( ID, [Group], [Status]) select 2, 'Gamma', 'ENABLED'
insert into @Values ( ID, [Group], [Status]) select 2, 'Beta', 'ENABLED'


update @Table 
set
AlphaStatus = (case when [Group] = 'Alpha' then [Status] else AlphaStatus end),
BetaStatus = (case when [Group] = 'Beta' then [Status] else BetaStatus end),
GammaStatus = (case when [Group] = 'Gamma' then [Status] else GammaStatus end)
from @Table t inner join @Values r 
on r.id = t.ID

select *From @Table

3 个答案:

答案 0 :(得分:2)

你可以用两种不同的方式来写这个:

第一种方法:

UPDATE t
SET t.AlphaStatus = coalesce(ra.[Status], t.AlphaStatus)
    ,t.BetaStatus = coalesce(rb.[Status], t.BetaStatus)
    ,t.GammaStatus = coalesce(rg.[Status], t.GammaStatus)
FROM @Table t
LEFT JOIN @values ra ON ra.id = t.ID
    AND ra.[group] = 'Alpha'
LEFT JOIN @values rb ON rb.id = t.ID
    AND rb.[group] = 'Beta'
LEFT JOIN @values rg ON rg.id = t.ID
    AND rg.[group] = 'Gamma';

第二种方法:

update t
set t.AlphaStatus = coalesce(r.[Status],t.AlphaStatus) 
from @Table t 
left join @Values r 
on r.id = t.ID
and [group] = 'Alpha';


update t
set t.BetaStatus = coalesce(r.[Status],t.BetaStatus) 
from @Table t 
left join @Values r 
on r.id = t.ID
and [group] = 'Beta';

update t
set t.GammaStatus = coalesce(r.[Status],t.GammaStatus) 
from @Table t 
left join @Values r 
on r.id = t.ID
and [group] = 'Gamma';

select * from @Table

SQL Fiddle Demo

答案 1 :(得分:2)

通过将每个字段分成不同的连接,您可以在一次更新中完成。

update t 
set
    AlphaStatus = ISNULL(rA.[Status], AlphaStatus),
    BetaStatus = ISNULL(rB.[Status], BetaStatus),
    GammaStatus = ISNULL(rG.[Status], GammaStatus)
from @Table t
left join @Values rA on rA.id = t.ID AND rA.[Group] = 'Alpha'
left join @Values rB on rB.id = t.ID AND rB.[Group] = 'Beta'
left join @Values rG on rG.id = t.ID AND rG.[Group] = 'Gamma'

答案 2 :(得分:2)

作为没有一堆左连接的单个更新,您可以将您的Values表分解为数据透视表(在公共表表达式内)并加入类似列名称:

;with CTE
as (
    select [ID]
         , [Alpha] as AlphaStatus
         , [Beta]  as BetaStatus
         , [Gamma] as GammaStatus
    from (
          select [ID]
               , [Group]
               , [Status]
            from @Values
         ) Source
    pivot (
           min(Status)
           for [group] in ([Alpha],[Beta],[Gamma])
          ) as pvt
   )
update @Table
   set AlphaStatus = coalesce(CTE.AlphaStatus,T.AlphaStatus)
     , BetaStatus  = coalesce(CTE.BetaStatus,T.BetaStatus)
     , GammaStatus = coalesce(CTE.GammaStatus,T.GammaStatus)
  from @Table T
  join CTE
    on CTE.[ID] = T.[ID]