[英]How can I insert into from one table to another with autoincrement in SQL Server
If for example I have these 2 tables in SQL Server:例如,如果我在 SQL 服务器中有这两个表:
Table 1 || Table 2
--------------------------||----------------------------
Number | Name || Number | Name
--------------|-----------|| --------------|------------
1 | B || 1 | A
2 | C || |
What I am trying to do is to insert the entries from table 1 to table 2, but I want table 2 to autoincrement the Number.我想做的是将表 1 中的条目插入到表 2 中,但我希望表 2 自动递增 Number。 So I want it to become like this:所以我希望它变成这样:
Table 2 ||
--------------------------||
Number | Name ||
--------------|-----------||
1 | A ||
2 | B ||
3 | C ||
I tried queries like this but it didn't work:我试过这样的查询,但没有用:
Insert into table2 (Number, Name)
select
(select max(number) + 1 from table1), Name
from table1
Maybe you will suggest to make the number in table2 primary key, but I want to do it using the max number like the query above.也许您会建议在 table2 中创建主键中的数字,但我想像上面的查询一样使用最大数字来做到这一点。
Thanks in advance.提前致谢。
Table2.Number should be IDENTITY
. Table2.Number 应该是IDENTITY
。 You can then just insert the names from table 1 in table 2.然后,您可以将表 1 中的名称插入表 2 中。
create table table2(number int not null identity(1,1), name char(1));
insert into table2 values ('A');
insert into table2 select name from table1;
This might be a bit too simplistic, and maybe you're looking for something more sophisticated, but...这可能有点太简单了,也许你正在寻找更复杂的东西,但是......
INSERT INTO Table2 (Number, Name)
SELECT
T.Number + X.MaxRowNumber
,T.Name
FROM Table1 T
INNER JOIN (SELECT MAX(Number) AS MaxRowNumber FROM Table2) X
ON 1 = 1
;
This approach is based on your statement:这种方法基于您的陈述:
Maybe you will suggest to make the number in table2 primary key, but I want to do it using the max number like the query above.也许您会建议在 table2 主键中设置数字,但我想使用上面查询的最大数字来完成。
I understood from this that you don't want to set Table2.Number as an IDENTITY column, but rather to perform the autoincrement during the INSERT operation.我从中了解到,您不想将 Table2.Number 设置为 IDENTITY 列,而是在 INSERT 操作期间执行自动增量。
I tend to agree with the other answers saying that Table2.Number should be an IDENTITY column and thus auto-increment itself without the need to calculate it, but if that is not what you want, then this answer should help you.我倾向于同意其他答案,即 Table2.Number 应该是 IDENTITY 列,因此无需计算即可自动递增,但如果这不是您想要的,那么这个答案应该对您有所帮助。
You can test this using table variables with real data to make sure it's what you're looking for:您可以使用带有真实数据的表变量对此进行测试,以确保它是您要查找的内容:
DECLARE @TABLE1 AS TABLE (Number INT, Name CHAR(1));
DECLARE @TABLE2 AS TABLE (Number INT, Name CHAR(1));
INSERT INTO @TABLE1 (Number, Name) VALUES ('1','B'), ('2','C');
INSERT INTO @TABLE2 (Number, Name) VALUES ('1','A');
INSERT INTO @TABLE2 (Number, Name)
SELECT
T.Number + X.MaxRowNumber
,T.Name
FROM @TABLE1 T
CROSS APPLY (SELECT MAX(Number) AS MaxRowNumber FROM @TABLE2) X
;
SELECT * FROM @TABLE2
;
You'll want to use INSERT INTO SELECT syntax like below to achieve this.您需要使用如下所示的 INSERT INTO SELECT 语法来实现此目的。
INSERT INTO table2
SELECT MAX(Number) + 1,
Name
FROM Table1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.