Solved

Subquery returned more than 1 value Workaround

Posted on 2009-05-11
7
281 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

839 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