Yury Merezhkov
asked on
Subquery returned more than 1 value Workaround
Hi guys,
I have two tables Products and Cart. Products have QuantityInStock field. Cart has Quantity field. After a customer pays for an order I need to update QuantityInStock in Products table based on the quantities of items in Cart. I have the following statement. It works fine when there is only one type of item in Cart (It can have > 1 quantity). However, if I add a different item I get "Subquery returned more than 1 value," of course. How can I do this differently?
Thank you.
I have two tables Products and Cart. Products have QuantityInStock field. Cart has Quantity field. After a customer pays for an order I need to update QuantityInStock in Products table based on the quantities of items in Cart. I have the following statement. It works fine when there is only one type of item in Cart (It can have > 1 quantity). However, if I add a different item I get "Subquery returned more than 1 value," of course. How can I do this differently?
Thank you.
Dim strSQL0 As String = "UPDATE Products SET Products.QuantityInStock = Products.QuantityInStock-" & _
"(SELECT Quantity FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "') WHERE Products.ProductID" & _
"=(SELECT ProductFK1 FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "')"
Hi RealSnaD,
You are trying to reduce the quantity in stock by the returned value? ... am I understanding correctly? If so, try ..
lwadwell
You are trying to reduce the quantity in stock by the returned value? ... am I understanding correctly? If so, try ..
lwadwell
Dim strSQL0 As String = "UPDATE Products SET Products.QuantityInStock = Products.QuantityInStock-" & _
"(SELECT SUM(Quantity) FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "') WHERE Products.ProductID" & _
"=(SELECT ProductFK1 FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "')"
ASKER
I get the same error after changing it to:
Dim strSQL0 As String = "UPDATE Products SET Products.QuantityInStock=Products.QuantityInStock-" & _
"(SELECT Quantity FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "') WHERE Products.ProductID IN " & _
"(SELECT ProductFK1 FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "')"
ASKER
lwadwell, thank you but products in cart are different and I cannot use SUM, therefore. Each product in the cart has its own quantity which needs to be deducted from QuantityInStock of the corresponding product in Products table.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
RealSnaD,
oh, ok ... sounds like you need a predicate in the
SELECT Quantity FROM Cart WHERE Cart.CustomerID=
to limit it to the product in question.
Perhaps ...
lwadwell
oh, ok ... sounds like you need a predicate in the
SELECT Quantity FROM Cart WHERE Cart.CustomerID=
to limit it to the product in question.
Perhaps ...
lwadwell
Dim strSQL0 As String = "UPDATE Products SET Products.QuantityInStock = Products.QuantityInStock-" & _
"(SELECT SUM(Quantity) FROM Cart WHERE Products.ProductID = Cart.ProductFK1 AND Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "') WHERE Products.ProductID" & _
"=(SELECT ProductFK1 FROM Cart WHERE Cart.CustomerID='" & _
Membership.GetUser.ProviderUserKey.ToString() & "')"
ASKER
Worked like a charm! Thank a lot.
In this case all elements in the subquery will be tested
Open in new window