错误::删除语句中查询失败
我有一个要求,我希望根据表 A 中的条件选择行,并且必须删除表 B。
例如EMP和EMP1是两个表
Merge into emp1 a
using (select * from emp) b
on (a. empno =b.empno)
WHEN MATCHED THEN DELETE
where(b.LOC='NEW YORK');
上面的查询结果出错。 如果我使用 Where contains
,则表 A
中的所有行都会被删除,这不是正确的解决方案。
delete from emp1 a
where exists
( select null
from emp b
where a. empno =b.empno
and b.LOC='NEW YORK'
);
请建议
I have a requirement that I want rows selected based on a condition from table A and table B must be deleted.
For example EMP and EMP1 are two tables
Merge into emp1 a
using (select * from emp) b
on (a. empno =b.empno)
WHEN MATCHED THEN DELETE
where(b.LOC='NEW YORK');
The above query results in error.
If I use Where exists
, all rows are deleted in Table A
, which is not a right solution.
delete from emp1 a
where exists
( select null
from emp b
where a. empno =b.empno
and b.LOC='NEW YORK'
);
Please suggest
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 MERGE 语句,但我会说 emp b 没有正确声明,因为 b 位于括号之外。
另外,我建议首先创建一个视图(或临时连接表),以便能够在执行 DELETE 命令之前查看哪些项目匹配。然后您可以基于视图使用 DELETE 并轻松地从两个表中删除。
I am not familiar with the MERGE statement, but I will say that emp b isn't properly declared because the b is outside the parenthesis.
Also, I would suggest first making a view (or a temporarily joined table) to be able to see what items match up before executing a DELETE command. Then you can use DELETE based on the view and easily delete from both tables.