繁体   English   中英

db2使用insert语句设置增量值

[英]db2 set incrementing values using insert statement

我有两个表:

MYTABLE1
ID   |Col1|Col2      |
--------------------- ....
64  |  50 |   7000   |



MYTABLE2
MY_ID |
-------
  64  | 
  87  |

我需要向MYTABLE1插入10,000行,其中ID = MY_ID Col1 = 50 ant Col2每次插入行时都会递增,因此MYTABLE1看起来像这样:

ID   |Col1|Col2      |
--------------------- ....
64  |  50 |   7000   |
87  |  50 |   7001   |

我在这里找到了类似的解决方案: 在DB2中插入数千行

但我仍然不知道如何从MYTABLE2加载所有ID

请帮助,谢谢您的回答。

insert into MYTABLE1 (id, col1, col2)
select my_id, 50 col1, col2 + row_number() over(order by my_id) from
(
    select MY_ID, m.col2
    from MYTABLE2 t2, (select max(col2) as col2 from MYTABLE1) m
) t2
-- if you don't want to insert IDs which already exist in MYTABLE1
where not exists (select 1 from MYTABLE1 t1 where t1.ID = t2.MY_ID);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM