我需要更新数据库表中的一些行。如何识别要更新的行涉及一系列复杂的语句,我设法将它们归结为一系列WITH语句。现在我有了正确的数据值,我需要更新表。
由于我设法使用WITH语句获取这些值,因此我希望在更新/合并中使用它。一个简化的示例如下:
with data1
(
ID_1
)
as
(
Select ID
from ID_TABLE
where ID > 10
)
,
cmedb.data2
(
MIN_ORIGINAL_ID
,OTHER_ID
)
as
(
Select min(ORIGINAL_ID)
,OTHER_ID
from OTHER_ID_TABLE
where OTHER_ID in
(
Select distinct ID_1
From data1
)
group by OTHER_ID
)
select MIN_ORIGINAL_ID
,OTHER_ID
from cmedb.data2
现在我有两列数据,我想用它们来更新表。因此,我没有将选择放在底部,而是尝试了各种合并和更新的组合,包括将WITH语句放在UPDATE/MERGE之上,或者作为UPDATE/MERGE语句的一部分。以下是我心中最接近我想做的事情:
merge into ID_TABLE as it
using
(
select MIN_ORIGINAL_ID
,OTHER_ID
from cmedb.data2
) AS SEL
ON
(
it.ID = sel.OTHER_ID
)
when matched then
update
set it.ORIGINAL_ID = sel.MIN_ORIGINAL_ID
所以它不起作用。我不确定这是否可能,因为我在互联网上没有找到使用WITH语句与UPDATE或MERGE结合使用的示例。我有WITH语句与INSERT结合使用的例子,所以相信这是可能的。
如果有人可以提供帮助,那就太好了,如果我遗漏了任何对解决问题有用的信息,请告诉我。
免责声明:我提供的示例是我想要做的事情的简化版本,实际上可能没有任何意义!
I need to update some rows in a DB table. How I identify the rows to be updated involved a series of complicated statements, and I managed to boil them down to a series of WITH statements. Now I have the correct data values, I need to update the table.
Since I managed to get these values with a WITH statement, I was hoping to use it in the UPDATE/MERGE. A simplified example follows:
with data1
(
ID_1
)
as
(
Select ID
from ID_TABLE
where ID > 10
)
,
cmedb.data2
(
MIN_ORIGINAL_ID
,OTHER_ID
)
as
(
Select min(ORIGINAL_ID)
,OTHER_ID
from OTHER_ID_TABLE
where OTHER_ID in
(
Select distinct ID_1
From data1
)
group by OTHER_ID
)
select MIN_ORIGINAL_ID
,OTHER_ID
from cmedb.data2
Now I have the two columns of data, I want to use them to update a table. So instead of having the select at the bottom, I've tried all sorts of combinations of merges and updates, including having the WITH statement above the UPDATE/MERGE, or as part of the UPDATE/MERGE statement. The following is what comes closest in my mind to what I want to do:
merge into ID_TABLE as it
using
(
select MIN_ORIGINAL_ID
,OTHER_ID
from cmedb.data2
) AS SEL
ON
(
it.ID = sel.OTHER_ID
)
when matched then
update
set it.ORIGINAL_ID = sel.MIN_ORIGINAL_ID
So it doesn't work. I'm unsure if this is even possible, as I've found no examples on the internet using WITH statements in combination with UPDATE or MERGE. I have examples of WITH statements being used in conjunction with INSERT, so believe it might be possible.
If anyone can help it would be great, and please let me know if I've left out any information that would be useful to solve the problem.
Disclaimer: The example I've provided is a boiled down version of what I'm trying to do, and may not actually make any sense!
发布评论
评论(6)
正如 @Andrew White 所说,您不能在 MERGE 语句中使用公用表表达式。
但是,您可以消除带有嵌套子选择的公共表表达式。这是您的示例选择语句,使用嵌套子选择重写:
这有点复杂(确切的语句可以写得更好),但我意识到您只是给出了一个简化的示例。
您可以使用嵌套子选择而不是公用表表达式来重写 MERGE 语句。这在语法上当然是可能的。
例如:
同样,这很令人费解,但它向您表明它至少是可能的。
As @Andrew White says, you can't use a common table expression in a MERGE statement.
However, you can eliminate the common table expressions with nested subselects. Here is your example select statement, rewritten using nested subselects:
This is somewhat convoluted (the exact statement could be written better), but I realize that you were just giving a simplified example.
You may be able to rewrite your MERGE statement using nested subselects instead of common table expressions. It is certainly syntactically possible.
For example:
Again, this is convoluted, but it shows you that it is at least possible.
将 WITH 语句与 UPDATE(以及 INSERT)结合使用的一种方法是使用 SELECT FROM UPDATE 语句(在这里):
A way to use WITH statement with UPDATE (and INSERT too) is using SELECT FROM UPDATE statement (here):
我现在正在查找语法,但我很确定答案是否定的。至少在我上次使用的 DB2 版本中不是这样。查看 更新 和 合并 文档页面的语法。即使您在语法中看到fullselect,您也不能使用
with
,因为根据选择 文档页面。I'm looking up the grammar now but I am pretty sure the answer is no. At least not in the version of DB2 I last used. Take a peek at the update and merge doc pages for their syntax. Even if you see the fullselect in the syntax you can't use
with
as that is explicitly separate according to the select doc page.如果您运行的是 DB2 V8 或更高版本,这里有一个有趣的 SQL hack 此处允许您使用WITH语句在查询中更新/插入。对于插入件和需要大量初步数据准备的更新,我发现这种方法提供了很多清晰度。
编辑 这里有一个更正 - 我相信 V9 中引入了从 UPDATE 语句中进行选择,因此上述内容适用于 V8 或更高版本的插入以及 V9 或更高版本的更新。
If you're running DB2 V8 or later, there's an interesting SQL hack here that allows you to UPDATE/INSERT in a query with a WITH statement. For inserts & updates that require a lot of preliminary data prepping, I find this method offers a lot of clarity.
Edit One correction here - selecting from UPDATE statements was introduced in V9 i believe, so the above will work for inserts on V8 or greater, and updates for V9 or greater.
将 CTE 放入视图中,然后从合并视图中进行选择。这样你就可以获得一个干净、可读的视图,以及一个干净、可读的合并。
Put the CTEs into a view, and select from the view in the merge. You get a clean, readable view that way, and a clean, readable merge.
另一种方法是简单地替换您的WITH 查询并仅使用子选择。
例如,如果您有(并且我试图包含一个有点复杂的示例,其中包含一些 WHERE 逻辑、聚合函数(MAX)和 GROUP BY,只是为了向您展示更真实的世界):
...您可以将其变成某种东西通过执行以下操作适合 MERGE INTO:
最后,您将得到一个如下所示的直接 SELECT 查询(重新格式化以显示正确的缩进):
我已经根据自己的个人经验(实际上现在)完成了此操作,并且效果非常好。
祝你好运。
Another method is to simply substitute your WITH queries and just use subselects.
For example, if you had (and I tried to include a somewhat complex example with some WHERE logic, an aggregate function (MAX) and a GROUP BY, just to show it more real world):
... you could turn this into something appropriate for a MERGE INTO by doing the following:
In the end, you'll have a straight SELECT query that looks like this (reformatted to show proper indentation):
I have done this in my own personal experience (just now actually) and it works perfectly.
Good luck.