[英]db2 set incrementing values using insert statement
I have two tables: 我有两个表:
MYTABLE1
ID |Col1|Col2 |
--------------------- ....
64 | 50 | 7000 |
MYTABLE2
MY_ID |
-------
64 |
87 |
and i need to insert like 10,000 rows to MYTABLE1 where ID=MY_ID Col1=50 ant Col2 increments each time the row is inserted so MYTABLE1 would look like that: 我需要向MYTABLE1插入10,000行,其中ID = MY_ID Col1 = 50 ant Col2每次插入行时都会递增,因此MYTABLE1看起来像这样:
ID |Col1|Col2 |
--------------------- ....
64 | 50 | 7000 |
87 | 50 | 7001 |
I have found similar solution here: insert thousands of rows in DB2 我在这里找到了类似的解决方案: 在DB2中插入数千行
but i still cant figure out how to load all the IDs from MYTABLE2 但我仍然不知道如何从MYTABLE2加载所有ID
Please help, Thank You for Your answers. 请帮助,谢谢您的回答。
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.