Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is my SQL query.

SQL
"select Unit from UnitConversion "+
               "where uocNo=(select uocNo from UnitConversionProduct "+
               "where UnitConversionProduct.ProductNo=@ProductNo)"


UnitConversion table has following columns
uocNo
Unit
Amount
Base

UnitConversionProduct table has following columns
uocNo
ProductNo


when i execute that query the following error appear

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."
Posted
Comments
vinodkumarnie 6-Apr-13 13:11pm    
It throws an error means there is mistake in your Syntax. Verify the datatype and then try..

When you use =,!=, <, <= , >, >= in where clause you can compare it(here uocNo) to only one value , but in your query it`s return more than one value.(Try to run this
SQL
select uocNo from UnitConversionProduct "+
               "where UnitConversionProduct.ProductNo=@ProductNo

you will get more than one value.) For solutions you have to ensure that subquery must return only one value. I think you must have multiple value for single ProductNo.
 
Share this answer
 
Error message you are getting is very well self explanatory. Its failing because your SubQuery returning more than one value. Try either of below two queries.

SQL
SELECT Unit FROM UnitConversion where uocNo in(select uocNo from UnitConversionProduct WHERE UnitConversionProduct.ProductNo=@ProductNo)

OR
SQL
SELECT Unit FROM UnitConversion UC INNER JOIN UnitConversionProduct UCP ON UC.uocNo = UCP.uocNo WHERE UCP.ProductNo = @ProductNo


I recommend you to use second query becuase Inner Join is faster in performance as compare to SubQuery.

Hope it helps!
 
Share this answer
 
Comments
deva936 7-Apr-13 1:50am    
thanks a lot. i will use second one
You can use in subquery:
1) TOP()[^] statement
SQL
SELECT Unit
FROM UnitConversion
    WHERE uocNo=(SELECT TOP(1) uocNo
                 FROM UnitConversionProduct
                 WHERE UnitConversionProduct.ProductNo=@ProductNo)


or
2) DISTINCT[^] statement
SQL
SELECT Unit
FROM UnitConversion
    WHERE uocNo=(SELECT DISTINCT uocNo
                 FROM UnitConversionProduct
                 WHERE UnitConversionProduct.ProductNo=@ProductNo)


In most cases using TOP and/or DISTINCT should helps to return 1 record.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900