删除多个表中的记录

发布于 2024-09-15 11:21:18 字数 444 浏览 5 评论 0原文

考虑以下几个表:

tab1
-------
userid
email
address
environment

tab2

-------
ecode
company
policy

tab3
-------
id
pan no.
dl no.

这里 tab1 是父表。我想从 tab2tab3 中删除 tab1 的所有 useriduserid 将位于 tab1tab2 或两者中。这些表是特定于环境的,即 environment/tab1environment/tab2

如何通过单个查询来完成此操作?

Consider these several tables:

tab1
-------
userid
email
address
environment

tab2

-------
ecode
company
policy

tab3
-------
id
pan no.
dl no.

Here tab1 is the parent table. I want to delete all the userid of tab1 from tab2 and tab3. Either userid will be in tab1 or tab2 or both. These tables are environment specific means environment/tab1 and environment/tab2.

How can this be done with a single query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

在梵高的星空下 2024-09-22 11:21:18

我不是 AS400 的专家,但我不知道有什么方法可以在任何 SQL 方言中做到这一点。您需要启动一个事务,执行单独的删除,然后如果您想确保该操作是原子的(意味着整个操作运行或都不运行),则提交该事务。

代码可能看起来像这样:

BEGIN TRANSACTION

DELETE FROM tab2
WHERE userid IN (SELECT userid FROM tab1)

DELETE FROM tab3
WHERE userid IN (SELECT userid FROM tab1)

COMMIT TRANSACTION

您没有具体指定这些表是如何相关的,所以我进行了猜测。另外,您可能应该进行一些错误处理,以便在遇到错误时发出ROLLBACK。我不知道 AS400(DB2?)的错误处理语法,因此您需要查找它。

I'm not an expert with the AS400, but I don't know of any way to do that in any SQL dialect. You need to start a transaction, perform your separate deletes, then commit the transaction if you want to make sure that it the action is atomic (meaning that the whole thing runs or none of it runs).

The code would probably look something like this:

BEGIN TRANSACTION

DELETE FROM tab2
WHERE userid IN (SELECT userid FROM tab1)

DELETE FROM tab3
WHERE userid IN (SELECT userid FROM tab1)

COMMIT TRANSACTION

You don't specify exactly how those tables are related, so I took a guess. Also, you should probably have some error handling that issues a ROLLBACK if an error is encountered. I don't know the error handling syntax for AS400 (DB2?), so you'll need to look that up.

远山浅 2024-09-22 11:21:18

环境是否在同一台机器上?如果没有,您可能需要查看 DDM 文件。
请参阅此处

Are the environments on the same machine? If not you might want to look at DDM files.
See here.

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