运行查询时出错:
System.Data.SqlClient.SqlException(0x80131904):子查询返回了多个值。当子查询遵循=,!=,<,<=,>,> =或将子查询用作表达式时,不允许这样做
这是我的代码:
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)
如何解决?
答案 0 :(得分:1)
首先,您应该使用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
中的单个记录匹配。目前还不清楚如何解决该问题。这将使用任意匹配行中的值进行更新。
答案 1 :(得分:0)
为您自己尝试,请在不确定top 1
返回的update
的数量rows
时在subquery
查询中选择 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)
:-
window.location.href = window.location.href; it may work for you.