Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server DELETE error - Subquery returned more than 1 value

Tags:

sql

sql-server

I having an error when trying to delete rows using a subquery:

DELETE FROM tblOrderDetails
WHERE  ProductID = (SELECT ProductID FROM tblProducts WHERE Discontinued = 1)

The error which I understand is:

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

I know the subquery is returning several values, but how can I then delete those rows Thanks any help would be appreciated. Patrick

like image 294
Patrick Avatar asked Dec 04 '22 21:12

Patrick


2 Answers

Try this

DELETE FROM tblOrderDetails 
WHERE ProductID IN (
    SELECT ProductID 
    FROM tblProducts 
    WHERE Discontinued = 1
)
like image 154
leoinfo Avatar answered Dec 21 '22 23:12

leoinfo


Use in:

DELETE FROM tblOrderDetails WHERE ProductID IN (SELECT ProductID FROM tblProducts WHERE Discontinued = 1)
like image 31
Christopher Armstrong Avatar answered Dec 21 '22 23:12

Christopher Armstrong