错误::删除语句中查询失败

发布于 2024-12-04 14:16:46 字数 493 浏览 3 评论 0原文

我有一个要求,我希望根据表 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜唯美灬不弃 2024-12-11 14:16:46
DELETE FROM emp1 a 
WHERE a.empno IN 
      ( SELECT b.empno 
        FROM emp b 
        WHERE b.LOC = 'NEW YORK' 
      );
DELETE FROM emp1 a 
WHERE a.empno IN 
      ( SELECT b.empno 
        FROM emp b 
        WHERE b.LOC = 'NEW YORK' 
      );
生活了然无味 2024-12-11 14:16:46

我不熟悉 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文