组合更新查询与单个更新语句

时间:2022-11-28 01:32:14

Can any Body suggest how to make the below 4 update statement to single update statement.

任何Body都可以建议如何将以下4更新语句转换为单个更新语句。

update dbo.FACOD set LowValue ='07/02/2010' where Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2012-13'
update dbo.FACOD set LowValue ='07/02/2011' where Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2013-14'

update dbo.FACOD set HighValue='06/30/2015' where Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2012-13'
update dbo.FACOD set HighValue='06/30/2016' where Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2013-14'

tried with below one

试过下面的一个

UPDATE  dbo.FACOD
SET     LowValue = CASE WHEN Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2012-13' THEN '07/02/2010' ELSE LowValue END,
        HighValue = CASE WHEN Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2012-13'THEN '06/30/2015'ELSE HighValue END,

But it is not satisfying all the update statement.

但它并不满足所有的更新声明。

2 个解决方案

#1


3  

Please try:

update 
    dbo.FACOD 
set 
    LowValue=(case when Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2012-13' then '07/02/2010' 
                    when Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2013-14' then '07/02/2011' 
                else LowValue end),

    HighValue=(case when Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2012-13' then '06/30/2015' 
                    when Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2013-14' then '06/30/2016' 
                else HighValue end)
where 
    Fieldname in ('AYStartDate', 'Dateloanstart', 'AYEndDate', 'DateloanEnd')and 
    AwardYear IN ('2012-13', '2013-14')

#2


0  

Here is the Correct ANswer

这是正确的答案

UPDATE dbo.FACOD
SET LowValue = CASE 
        WHEN Fieldname IN (
                'AYStartDate'
                ,'Dateloanstart'
                )
            AND AwardYear = '2012-13'
            THEN '07/02/2010'
        WHEN Fieldname IN (
                'AYStartDate'
                ,'Dateloanstart'
                )
            AND AwardYear = '2013-14'
            THEN '07/02/2011'
        ELSE LowValue
        END
    ,HighValue = CASE 
        WHEN Fieldname IN (
                'AYEndDate'
                ,'DateloanEnd'
                )
            AND AwardYear = '2012-13'
            THEN '06/30/2015'
        WHEN Fieldname IN (
                'AYEndDate'
                ,'DateloanEnd'
                )
            AND AwardYear = '2013-14'
            THEN '06/30/2016'
        ELSE HighValue
        END
WHERE Fieldname IN (
        'AYStartDate'
        ,'Dateloanstart'
        ,'AYEndDate'
        ,'DateloanEnd'
        )
    AND AwardYear IN (
        '2012-13'
        ,'2013-14'
        )

#1


3  

Please try:

update 
    dbo.FACOD 
set 
    LowValue=(case when Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2012-13' then '07/02/2010' 
                    when Fieldname in ('AYStartDate','Dateloanstart')and AwardYear='2013-14' then '07/02/2011' 
                else LowValue end),

    HighValue=(case when Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2012-13' then '06/30/2015' 
                    when Fieldname in ('AYEndDate','DateloanEnd')and AwardYear='2013-14' then '06/30/2016' 
                else HighValue end)
where 
    Fieldname in ('AYStartDate', 'Dateloanstart', 'AYEndDate', 'DateloanEnd')and 
    AwardYear IN ('2012-13', '2013-14')

#2


0  

Here is the Correct ANswer

这是正确的答案

UPDATE dbo.FACOD
SET LowValue = CASE 
        WHEN Fieldname IN (
                'AYStartDate'
                ,'Dateloanstart'
                )
            AND AwardYear = '2012-13'
            THEN '07/02/2010'
        WHEN Fieldname IN (
                'AYStartDate'
                ,'Dateloanstart'
                )
            AND AwardYear = '2013-14'
            THEN '07/02/2011'
        ELSE LowValue
        END
    ,HighValue = CASE 
        WHEN Fieldname IN (
                'AYEndDate'
                ,'DateloanEnd'
                )
            AND AwardYear = '2012-13'
            THEN '06/30/2015'
        WHEN Fieldname IN (
                'AYEndDate'
                ,'DateloanEnd'
                )
            AND AwardYear = '2013-14'
            THEN '06/30/2016'
        ELSE HighValue
        END
WHERE Fieldname IN (
        'AYStartDate'
        ,'Dateloanstart'
        ,'AYEndDate'
        ,'DateloanEnd'
        )
    AND AwardYear IN (
        '2012-13'
        ,'2013-14'
        )