How can I have two columns in SQL Server auto increment?

前端 未结 3 1320
死守一世寂寞
死守一世寂寞 2021-01-07 06:48

I have two columns in a table of a SQL server DB that I would like to autoincrement when new fields are added. However, Managment Studio wont allow me to set two columns to

3条回答
  •  星月不相逢
    2021-01-07 07:22

    There can only be one auto increment field per table. But, you could have a calculated field based on the auto increment field. Or, you could have an int field where you manage the sequence by front end code or by a trigger. And also you could use a sequence in SQL Server.

    CREATE SEQUENCE MySequence START WITH 100;
    
    CREATE TABLE MyTable
    (
        RealIdentity INT IDENTITY(1,1),
        RandomCol NVARCHAR(100),
        FakeIdentity INT DEFAULT NEXT VALUE FOR MySequence
    );
    

提交回复
热议问题