使用来自不同 table 的信息以增量更新 table

Update table with info from a different table with an increment

我想用 table 1 中的任何 ID 更新 table 2,其中 parentId = parentId

但仅限于我在 table 1

中的数量

因此,如果我在 table 1 5 条记录中有 parentId 1,则只更新 table2 5 条记录,其中 parentId = 1

Table 1:

id          NAME                                               ParentId
----------- -------------------------------------------------- -----------
1           Name1                                              1
2           Name2                                              1
3           Name3                                              1
4           Name4                                              1
5           Name5                                              1

Table 2:

Id          name                                               Table1Id    ParentId
----------- -------------------------------------------------- ----------- -----------
7           Name7                                              NULL        1
8           Name8                                              NULL        1
9           Name9                                              NULL        1
10          Name10                                             NULL        1
11          Name11                                             NULL        1
12          Name12                                             NULL        1
13          Name13                                             NULL        1
14          Name14                                             NULL        1

我需要的

Id          name                                               Table1Id    ParentId
----------- -------------------------------------------------- ----------- -----------
7           Name7                                              1           1
8           Name8                                              2           1
9           Name9                                              3           1
10          Name10                                             4           1
11          Name11                                             5           1
12          Name12                                             NULL        1
13          Name13                                             NULL        1
14          Name14                                             NULL        1

我尝试为每个行获取一个 rownum 并加入其中,但出现错误 Windowed functions can only appear in the SELECT or ORDER BY clauses.

嗯。 . .您需要生成一个密钥才能加入他们。使用 row_number():

with toupdate as (
      select t2.*,
             row_number() over (partition by parentid order by id) as seqnum
      from table2 t2
     )
update toupdate
    set Table1Id = t1.id
    from toupdate join
         (select t1.*,
                 row_number() over (partition by parentid order by id) as seqnum
          from table1 t1
         ) t1
         on t1.seqnum = t2.seqnum and t1.parentid = t2.parentid;

您需要枚举两个 table 中的行,然后更新。我喜欢使用可更新的 CTE 来完成这类任务,这些任务在 SQL 服务器中非常灵活:

with cte as (
    select t2.table1id, t1.id
    from (
        select t1.*, row_number() over (partition by parentid order by id) rn
        from table1 t1
    ) t1
    inner join (
        select t2.*, row_number() over (partition by parentid order by id) rn
        from table2 t2
    ) t2 on t2.parentid = t1.parentid and t2.rn = t1.rn
)
update cte set table1id = id

这种方法的好处是您不需要重新打开目标 table:每个 table 只扫描一次,然后进行更新。

Demo on DB Fiddle:

Id | name   | Table1Id | ParentId
-: | :----- | -------: | -------:
 7 | Name7  |        1 |        1
 8 | Name8  |        2 |        1
 9 | Name9  |        3 |        1
10 | Name10 |        4 |        1
11 | Name11 |        5 |        1
12 | Name12 |     null |        1
13 | Name13 |     null |        1
14 | Name14 |     null |        1