按顺序在 table 中插入数字,但不设置 IDENTITY

Insert numbers in table in sequence but without IDENTITY set

我有几个 tables,其中一个我从 excel 文件加载,然后我从 select 插入 在目标 table 中,我有列 id,上面没有 IDENTITY,我无法创建它,所以我在我的 select 中使用 ROW_NUMBER() 作为 INSERT, 但是现在我遇到了问题,下次我从文件加载到我的温度 table 时,我从那里 selection 数据 INSERT 行号从 1 开始,如果 iam尝试插入时我遇到了主键问题的错误/所以我需要根据目标中已经存在的 ID 添加 ID table 例如,如果在目标 table 中最后一条记录 1000 来自新插入的第一条记录必须有 id 1001

    INSERT  INTO table1
                    ( Id ,
                      OkeiId ,
                      OkpdId ,
                      OkvedId ,
                   )
                    SELECT 
                    --  (SELECT MAX (id) FROM table1) AS 'last id in table',
                    ROW_NUMBER() OVER ( ORDER BY ( SELECT 0 ) ) AS Row , -- Id - int
                            a.[Id] AS OkeId ,  - int
                            c.[Id] AS OkpId , - int
                            D.[Id] AS OkvId ,  - int

                    FROM    [dbo].#table b
                            LEFT OUTER JOIN table2 a ON b.F6 = a.[NationalSymbol]
                            LEFT OUTER JOIN table3 c ON b.F4 = c.Code
                            LEFT OUTER JOIN table4 D ON b.F5 = D.Code
                            LEFT OUTER JOIN table1 f ON b.f1 = f.Code
                    WHERE   f.code IS NULL

任何想法如何继续按顺序插入 id?我不能在 table 上使用 IDENTITY。希望你能理解我的解释

INSERT  INTO table1
                ( Id ,
                  OkeiId ,
                  OkpdId ,
                  OkvedId 
               )
                SELECT 
                --  (SELECT MAX (id) FROM table1) AS 'last id in table',
                a.last_id + ROW_NUMBER() OVER ( ORDER BY ( SELECT 0 ) ) AS Row , -- Id - int
                        a.[Id] AS OkeiId , -- OkeiId - int
                        c.[Id] AS OkpdId , -- OkpdId - int
                        D.[Id] AS OkvedId  -- OkvedId - int

                FROM    [dbo].#table b
                        LEFT OUTER JOIN table2 a ON b.F6 = a.[NationalSymbol]
                        LEFT OUTER JOIN table3 c ON b.F4 = c.Code
                        LEFT OUTER JOIN table4 D ON b.F5 = D.Code
                        LEFT OUTER JOIN table1 f ON b.f1 = f.Code
                        CROSS JOIN (SELECT MAX (id) AS last_id FROM table1 ) a
                WHERE   f.code IS NULL

和一些测试:

            CREATE TABLE #a
            (
                a INT PRIMARY KEY,
                aa int
            )

            CREATE TABLE #b
            (
                b int
            )

            INSERT INTO #a VALUES(1,1);

            INSERT INTO #b VALUES(1);
            INSERT INTO #b VALUES(2);
            INSERT INTO #b VALUES(3);
            INSERT INTO #b VALUES(4);
            INSERT INTO #b VALUES(5);

            INSERT INTO #a

            SELECT last_id + ROW_NUMBER() OVER(ORDER BY b.b), b.b FROM #b b CROSS JOIN (SELECT MAX(a) last_id FROM #a) a

如果您无法使用 IDENTITY for some reason and are on SQL Server 2012+, consider sequence numbers。它们可以与多个 columns/tables 一起使用,并且通常比 IDENTITY 更灵活。有关创建和使用的示例,请参见下文。

序列创建

CREATE SEQUENCE TestSequence
AS INTEGER
START WITH 1
INCREMENT BY 1;

序列使用

CREATE TABLE TestTable
    (
      TestId INTEGER NOT NULL,
      TestColumn CHAR(1) NULL
    );

INSERT INTO TestTable (TestId, TestColumn)
VALUES (NEXT VALUE FOR TestSequence, 'A');

SQL Fiddle