0

运行我的查询时出错:

System.Data.SqlClient.SqlException (0x80131904):子查询返回超过 1 个值。当子查询跟随 =、!=、<、<=、>、>= 或子查询用作表达式时,这是不允许的

这是我的代码:

UPDATE Trade
SET Reference = (SELECT DISTINCT temp.MainRecordNo
                 FROM temp
                 WHERE temp.SubRecordNo = Trade.TradeNo 
                   AND temp.LinkType = 'ATPD'
                   AND LinkStatus = 'A'
                   AND ISNULL(Trade.Reference, '') <> Temp.MainRecordNo)
WHERE EXISTS (SELECT DISTINCT Temp.MainRecordNo
              FROM Temp
              WHERE Temp.SubRecordNo = Trade.TradeNo
                AND Temp.LinkType = 'ATPD'
                AND LinkStatus = 'A'
                AND ISNULL(Trade.CstpReference, '') <> Temp.MainRecordNo)

如何解决?

4

2 回答 2

1

首先,您应该使用 a 重新表述这一点JOIN

UPDATE t
    set Reference = temp.MainRecordNo
FROM Trade t JOIN
     temp
     ON Temp.SubRecordNo = t.TradeNo
WHERE temp.LinkType = 'ATPD' AND
      temp.LinkStatus = 'A' AND  -- Guessing this comes from temp
      (t.CstpReference IS NULL OR t.CstpReference <> Temp.MainRecordNo);

这将解决您眼前的问题。您还有另一个问题,即 中的多条记录temp匹配Trade. 完全不清楚如何解决这个问题。这将使用来自任意匹配行的值进行更新。

于 2019-07-19T11:49:10.593 回答
0

只是为了你的答案试试这个,当你不确定你的回报数量时,top 1在查询中选择: -updaterowssubquery

    Update Trade
    set Reference = (Select top 1 distinct temp.MainRecordNo
                        from temp
                        where temp.SubRecordNo = Trade.TradeNo 
                        and temp.LinkType = 'ATPD'
                        and LinkStatus = 'A'
                        and isnull(Trade.Reference,'') <> Temp.MainRecordNo)
    WHERE EXISTS
      ( SELECT DISTINCT Temp.MainRecordNo
        FROM Temp
        where Temp.SubRecordNo = Trade.TradeNo
        and Temp.LinkType = 'ATPD'
        and LinkStatus = 'A'
        and isnull(Trade.CstpReference,'') <> Temp.MainRecordNo)
于 2019-07-19T11:51:01.557 回答