SQL Server DELETE 错误 - 子查询返回超过 1 个值

标签 sql sql-server

尝试使用子查询删除行时出现错误:

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

我理解的错误是:

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.

我知道子查询返回多个值,但是如何删除这些行 谢谢任何帮助,将不胜感激。 帕特里克

最佳答案

试试这个

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

关于SQL Server DELETE 错误 - 子查询返回超过 1 个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405096/

相关文章:

sql - DataGridView_CellValidating

sql - 子查询和相关子查询的区别

sql - 如何在 select 子句中使用 join in from 子句(如 SQL Server)执行 Postgresql 子查询?

sql - SQLite3 不支持外键约束吗?

MYSQL GUID 触发器

php - 将不同的输入值插入一列 (MySQL)

sql - 如何将不同和半不同的条目插入表中?

sql-server - 大表(10,000,000 行)上的 Nhibernate 分页性能

sql - 从一列中删除重复的行

c# - 在 sql server 中存储字母数字(如 pan 卡、选民 ID)的首选数据类型是什么?