简体   繁体   English

如何在 SQL 服务器中使用自动增量从一个表插入到另一个表

[英]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;

Fiddle 小提琴

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.

相关问题 使用T-Sql,如何从远程服务器上的一个表插入本地服务器上的另一个表? - Using T-Sql, how can I insert from one table on a remote server into another table on my local server? 如何使用插入查询将数据从一个 SQL 表插入另一个表 - How can I insert data from one SQL table to another table using Insert query 如何获取一个表的自动增量ID并插入另一个表 - how to get autoincrement ID of one table and insert in another table 如何将 SQL Server 存储过程的结果集从一台服务器导出到另一台服务器上的表中? - How can i export the result set of an SQL Server stored procedure from one server into a table on another server? 如何从表中删除一行并将其通过SQL语句插入另一行? - How can I remove one row from a table and insert it into another via a SQL statement? 如何使用游标 for 循环在 PL SQL 中将行从一个表插入到另一个表? - How can I insert rows from one table to another in PL SQL using the cursor for loop? 如何使用SQL Select插入将行从一个表复制到另一个表 - How can i use SQL Select Insert to copy rows from one table to another 如何在SQL Server中将一列从一个表复制到另一表 - How can I copy one column to from one table to another in SQL Server SQL Server:从一个表到另一个表的INSERT - SQL Server : INSERT from one table to another table SQL-SERVER 程序如何在一个表中插入行,然后将行插入到另一个表中作为外键链接到另一个表 - SQL-SERVER Procedure How do i insert row one table then insert row into another table link to another table as foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM