Link to home
Start Free TrialLog in
Avatar of Yury Merezhkov
Yury MerezhkovFlag for United States of America

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.

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() & "')"

Open in new window

Avatar of allmer
allmer
Flag of Türkiye image

Try changing the = to IN

In this case all elements in the subquery will be tested
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() & "')"

Open in new window

Avatar of Lee Wadwell
Hi RealSnaD,

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() & "')"

Open in new window

Avatar of Yury Merezhkov

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() & "')"

Open in new window

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
Avatar of RiteshShah
RiteshShah
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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() & "')"

Open in new window

Worked like a charm! Thank a lot.