子查询返回的值超过1。

时间:2012-11-29 15:07:42

标签: sql sql-server

运行此查询后,我收到以下错误

UPDATE stockloan_feed 
SET CUSIP =( 
      SELECT CUSIP FROM stockloan_feed_cusip_matching 
      WHERE stockloan_feed_cusip_matching.SEDOL = stockloan_feed.SEDOL OR       
      stockloan_feed_cusip_matching.ISIN = stockloan_feed.ISIN) 
WHERE CUSIP IS NULL and date = DATEDIFF(d, 0, GETDATE()-1)

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

2 个答案:

答案 0 :(得分:2)

UPDATE t1
SET t1.CUSIP = t2.CUSIP
FROM  stockloan_feed t1
INNER JOIN stockloan_feed_cusip_matching t2 
        ON t2.SEDOL = t1.SEDOL 
        OR t2.ISIN  = t1.ISIN
WHERE t1.CUSIP IS NULL
  AND t1.date = DATEDIFF(d, 0, GETDATE()-1)

答案 1 :(得分:1)

很简单,您尝试将列值分配给子查询的结果,但子查询返回的值不止一个。

您可以尝试指定您只想要子查询的第一个结果:

UPDATE stockloan_feed
SET    CUSIP = (SELECT TOP 1 CUSIP
                FROM   stockloan_feed_cusip_matching
                WHERE  stockloan_feed_cusip_matching.SEDOL = stockloan_feed.SEDOL
                        OR stockloan_feed_cusip_matching.ISIN = stockloan_feed.ISIN)
WHERE  CUSIP IS NULL
       AND date = Datediff(d, 0, Getdate() - 1)

这当然会假设您的子查询返回的所有可能值都相同,或者您只是不介意将哪一个匹配结果分配给您的stockloan_feed.CUSIP列。