SQL - 基于选择查询的更新表

时间:2016-03-01 18:20:12

标签: sql sql-server-2008

您好我需要根据条件使用两列中的值更新表。

所以我基本上需要做以下

更新表FundPriceDetails中的SecurityCode和Exchange列 其中isincode =' ES06139009N6' ' GB00BYMT2284' ' GB00BYNFF262&#39)

我在下面的查询中有isin到Exchange的映射。需要添加另一个列,它将对SecurityCode AS WELL做好

SELECT x.*
FROM (SELECT 'ES06139009N6' AS lsin, 'MAD' AS col2 UNION ALL
      SELECT 'GB0002634946', 'LSE' UNION ALL
      SELECT 'SG1L01001701', 'SGX'
     ) x JOIN
     FundPriceDetails fpd
     ON fpd.lsin = x.lsin;

如何使用这些值更新表

3 个答案:

答案 0 :(得分:1)

UPDATE
    FundPriceDetails 
SET
    FundPriceDetails .col1 = value,
    FundPriceDetails .col2 = value
FROM
    FundPriceDetails
INNER JOIN
    ( SELECT 'ES06139009N6' AS lsin, 'MAD' AS col2 UNION ALL
      SELECT 'GB0002634946', 'LSE' UNION ALL
      SELECT 'SG1L01001701', 'SGX'
     ) x 
ON
    FundPriceDetails.lsin = x.lsin;

答案 1 :(得分:1)

UPDATE  FundPriceDetails
 SET     SecurityCode = value,
        Exchange  = value
 WHERE   isincode in('ES06139009N6' ,'GB00BYMT2284' ,'GB00BYNFF262')

答案 2 :(得分:0)

你的问题不明确,但我认为你的意思是:

with myData (lsin, col2) as 
(
SELECT 'ES06139009N6', 'MAD' UNION ALL
      SELECT 'GB0002634946', 'LSE' UNION ALL
      SELECT 'SG1L01001701', 'SGX'
) 
update FundPriceDetails
set blahblahColumn = x.col2 
from myData x
where FundPriceDetails.lsin = x.lsin;