0
這個查詢工作,並返回260行:SQL子查詢返回多個值,沒有更新它工作正常
SELECT PRODUCTS.ProductResalePrice * ((100 - isnull((select discountperc from CUSTOMERDISCOUNT where CustomerID = CUSTOMERS.CustomerID and CategoryID = products.CategoryID), 0))/100) AS incDisc
FROM PRODUCTS
INNER JOIN SHIPMENTPRODUCTS op ON PRODUCTS.ProductID = op.ProductID
INNER JOIN SHIPMENTS ON op.shipmentid = shipments.shipmentid
INNER JOIN CUSTOMERS ON shipments.CustomerID = CUSTOMERS.CustomerID
left JOIN CUSTOMERDISCOUNT ON CUSTOMERS.CustomerID = CUSTOMERDISCOUNT.CustomerID
AND PRODUCTS.CategoryID = CUSTOMERDISCOUNT.CategoryID
WHERE op.shipmentid = 1234
這也是260行:
select * from SHIPMENTPRODUCTS where shipmentid = 1234
但下一個給:
Msg 512,Level 16,State 1,Line 2 子查詢返回的值超過1個。當子查詢遵循=,!=,<,< =,>,> =或當子查詢用作表達式時,這是不允許的。
update SHIPMENTPRODUCTS set SoldFor =
(SELECT PRODUCTS.ProductResalePrice * ((100 - isnull((select discountperc from CUSTOMERDISCOUNT where CustomerID = CUSTOMERS.CustomerID and CategoryID = products.CategoryID), 0))/100) AS incDisc
FROM PRODUCTS
INNER JOIN SHIPMENTPRODUCTS op ON PRODUCTS.ProductID = op.ProductID
INNER JOIN SHIPMENTS ON op.shipmentid = shipments.shipmentid
INNER JOIN CUSTOMERS ON shipments.CustomerID = CUSTOMERS.CustomerID
left JOIN CUSTOMERDISCOUNT ON CUSTOMERS.CustomerID = CUSTOMERDISCOUNT.CustomerID
AND PRODUCTS.CategoryID = CUSTOMERDISCOUNT.CategoryID
WHERE op.ProductID = SHIPMENTPRODUCTS.productid and op.shipmentid = SHIPMENTPRODUCTS.shipmentid)
where SHIPMENTPRODUCTS.shipmentid = @shipmentId
,我認真沒有任何線索。也不知道如何開始這個。區別僅在於查詢1是獨立的,查詢2是包裝在更新語句中的查詢1。
查詢1返回多於一個行,所以更新無法完成。你想添加260行到SHIPMENTPRODUCTS表中? – CiucaS 2014-10-22 10:53:10
在內部查詢中添加op.ShipmentId = @shipmentId也是。 – Roopesh 2014-10-22 11:05:01