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

前端 未结 3 1322
死守一世寂寞
死守一世寂寞 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:13

    If you wanted the 2nd column to basically be a mirror of the first:

    ALTER TABLE dbo.myTable ADD
       foo  AS [rowid]
    GO
    

    If you wanted it to apply some math formula to it to achieve some kind of offset:

    ALTER TABLE dbo.myTable ADD
        foo  AS ([rowid]+1) * 7 --or whatever you like.
    GO
    

提交回复
热议问题