Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >=

I run the following query:

SELECT     orderdetails.sku,    orderdetails.mf_item_number,    orderdetails.qty,    orderdetails.price,    supplier.supplierid,    supplier.suppliername,    supplier.dropshipfees,    cost = (SELECT supplier_item.price            FROM   supplier_item,                   orderdetails,                   supplier            WHERE  supplier_item.sku = orderdetails.sku                   AND supplier_item.supplierid = supplier.supplierid) FROM   orderdetails,        supplier,        group_master WHERE  invoiceid = '339740'        AND orderdetails.mfr_id = supplier.supplierid        AND group_master.sku = orderdetails.sku   

I get the following error:

Msg 512, Level 16, State 1, Line 2 Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Any ideas?

like image 254
Anilkumar Avatar asked Apr 16 '10 13:04

Anilkumar


People also ask

How do you avoid subquery returned more than 1 value this is not permitted?

This is not permitted when the subquery follows =, != , <, <= , >, >= or when the subquery is used as an expression.

Which operator will not accept more than one value from the subquery?

The operators used in a single-row subqueries relational operators (=, <>, >, >=, <, <=) cannot be used in multiple-row subqueries.

How many subqueries can be used in a single SQL statement?

A subquery can itself include one or more subqueries. Any number of subqueries can be nested in a statement.

Which operator is used in a WHERE clause when the subquery returns more than one row of data?

Multiple row subquery returns one or more rows to the outer SQL statement. You may use the IN, ANY, or ALL operator in outer query to handle a subquery that returns multiple rows.


1 Answers

Try this:

SELECT     od.Sku,     od.mf_item_number,     od.Qty,     od.Price,     s.SupplierId,     s.SupplierName,     s.DropShipFees,     si.Price as cost FROM     OrderDetails od     INNER JOIN Supplier s on s.SupplierId = od.Mfr_ID     INNER JOIN Group_Master gm on gm.Sku = od.Sku     INNER JOIN Supplier_Item si on si.SKU = od.Sku and si.SupplierId = s.SupplierID WHERE     od.invoiceid = '339740' 

This will return multiple rows that are identical except for the cost column. Look at the different cost values that are returned and figure out what is causing the different values. Then ask somebody which cost value they want, and add the criteria to the query that will select that cost.

like image 80
Jeffrey L Whitledge Avatar answered Sep 23 '22 12:09

Jeffrey L Whitledge