开发者

i want to insert values from table1 to table2 with incremantal value

i tried some开发者_Python百科thing like this but the id value is not changing, getting the value for first record and set the value for all rest...

insert into table1 (Id,b,c,d) 
 (select (select max(Id)+1 from table1),x,y,z from table2 where... )


Use an identity column. Then you can just do this:

INSERT INTO table1 (b, c, d) 
SELECT x, y, z
FROM table2
WHERE ...

If you don't want to use an auto-increment column you can get the same effect by using adding the row number of the row instead of always adding 1. This syntax works in almost all mainstream SQL databases (not MySQL though):

INSERT INTO table1 (Id, b, c, d) 
SELECT 
    (SELECT MAX(Id) FROM table1) + ROW_NUMBER() OVER (ORDER BY x),
    x, y, z
FROM table2
WHERE ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜