Auto increment primary key in SQL Server Management Studio 2012

前端 未结 11 1288
谎友^
谎友^ 2020-11-22 10:11

How do I auto increment the primary key in a SQL Server database table, I\'ve had a look through the forum but can\'t see how.

11条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 10:55

    Perhaps I'm missing something but why doesn't this work with the SEQUENCE object? Is this not what you're looking for?

    Example:

    CREATE SCHEMA blah.
    GO
    
    CREATE SEQUENCE blah.blahsequence
    START WITH 1
    INCREMENT BY 1
    NO CYCLE;
    
    CREATE TABLE blah.de_blah_blah
    (numbers bigint PRIMARY KEY NOT NULL
    ......etc
    

    When referencing the squence in say an INSERT command just use:

    NEXT VALUE FOR blah.blahsequence
    

    More information and options for SEQUENCE

提交回复
热议问题