有一个表,要新增一个自增的列,怎么实现,对于列的属性有什么要求 列一定要是int型的吗?
请各位指点一下小弟!

解决方案 »

  1.   

    alter table tb add id int identity
      

  2.   

    alter table TB add column id int identity(1,1)
      

  3.   

    alter table tb
    add id int identity --或者bigint
      

  4.   

    对于列的属性有什么要求 列一定要是int型的吗?
    int,bigint,dec(n)--不要带小数 等都可以。
      

  5.   

    感谢各位,但还有一事不明,这种自增列一定要设置为int的吗?
      

  6.   

    int,或bigint.或者参考如下:Id, FormatId, F1 ,F2
    Id序号我设了自动加一,FormatId我想他也象这样"SL000001",
    当Insert时就加1,FormatId我想他也能自动加一"SL000001","SL000002"...
    能用一条sql什么办法实现.最好不要用中间表。有什么好方法?
    谢谢!
    create table #test
    (id int identity,
    FormatId as 'SL'+right(10000000+id,6),
    F1 varchar(50))
    go
    insert #test(F1) select '1'
    union all select '2'
    select * from #testdrop table #test
    /*
    id          FormatId       F1   
    ----------- -------------- -----
    1           SL000001       1
    2           SL000002       2(所影响的行数为 2 行)
    */
      

  7.   

    declare @t table(id dec(20) identity(1,1),col varchar(10))
    insert @t select 'a'
    union all select 'b'select * from @t
    /*
    id                                      col
    --------------------------------------- ----------
    1                                       a
    2                                       b(2 行受影响)
    */
      

  8.   

    标识列 'id' 的数据类型必须是 int、bigint、smallint、tinyint 
    或 decimal,或者是小数位数为 0 的 numeric 数据类型,
    并且约束为不可为 Null。