0

I want to do an update on my table using the following CASE-WHEN logic:

UPDATE    table1
SET   Volume_eur =              
        CASE    


            WHEN TradeCode = 'A' THEN PaymentAmount_field1
            WHEN TradeCode = 'B' THEN PaymentAmount_field2
            WHEN TradeCode = 'C' THEN PaymentAmount_field3

             END                      

                  * CurrencyRate/100

FROM table1

But I want to document which logic was used and stamp that on the Statusfield. So basically I want to do an update-statement inside my CASE-WHEN sentence like this: (marked with bold)

UPDATE    table1
SET              Volume_eur = 

            CASE    


            WHEN TradeCode = 'A' THEN PaymentAmount_field1  



UPDATE table1 SET StatusField = 'Logic1'

            WHEN TradeCode = 'B' THEN PaymentAmount_field2



UPDATE table1 SET StatusField = 'Logic2'

            WHEN TradeCode = 'C' THEN PaymentAmount_field3



UPDATE table1 SET StatusField = 'Logic3'

             END                      

                  * CurrencyRate/100

  FROM table1

Is that possible, or how should I do it? I would like to avoid copying the whole CASE-WHEN logic since it will become harder to maintain it.

4

1 回答 1

1

如果您不想使用case 语句,则使用不同的内容进行 3 个单独的查询,如下所示:

UPDATE    table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field1 
where TradeCode = 'A';    

UPDATE    table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field2 
where TradeCode = 'B';

UPDATE    table1 set Volume_eur=PaymentAmount * CurrencyRate/100_field3 
where TradeCode = 'C';

希望这对您有所帮助!

于 2012-11-23T10:13:16.893 回答