按顺序在表格中插入数字但未设置IDENTITY

时间:2015-03-16 14:21:58

标签: sql-server identity sql-insert row-number

我有几张桌子,其中一张桌子我正在从excel文件加载,然后我从select中插入 在目标表格中,我的列id上没有IDENTITY,我无法创建它,因此我在ROW_NUMBER()的选择中使用了INSERT, 但是现在我遇到了问题,下次当我从文件加载到我的临时表时,我从INSERT行号的选择数据从1开始,如果iam尝试插入,我得到了错误主要问题的问题/所以我需要根据已经在目标表中的id添加ID 例如,如果在目标表中最后一条记录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?我不能在这张桌子上使用IDENTITY。我希望你能理解我的解释

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:2)

如果由于某些原因而无法使用IDENTITY并且使用的是SQL Server 2012+,请考虑sequence numbers。它们可以与多个列/表一起使用,并且通常比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