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

时间:2013-09-02 04:50:16

标签: sql sql-server

任何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'

试过下面的一个

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,

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

2 个答案:

答案 0 :(得分: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')

答案 1 :(得分: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'
        )