2

任何机构都可以建议如何将以下 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'

尝试以下一个

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,

但这并不能满足所有更新声明。

4

2 回答 2

3

请试试:

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')
于 2013-09-02T04:56:09.943 回答
0

这是正确的答案

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'
        )
于 2013-09-02T05:12:02.297 回答