Solved

Subquery returned more than 1 value Workaround

Posted on 2009-05-11
7
278 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:RealSnaD
7 Comments
 
LVL 5

Expert Comment

by:allmer
ID: 24361656
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

0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24361714
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

0
 
LVL 7

Author Comment

by:RealSnaD
ID: 24361725
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

0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Author Comment

by:RealSnaD
ID: 24361739
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.
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 500 total points
ID: 24361890
what about this query???

update products set product.quantityinstock=product.quantityinstock-cart.quantity
from products join cart
on products.productid=cart.productfk1 and cart.customerid='abc'
0
 
LVL 25

Expert Comment

by:lwadwell
ID: 24361896
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

0
 
LVL 7

Author Comment

by:RealSnaD
ID: 24361928
Worked like a charm! Thank a lot.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now