我有一个表比如a(id,name) 我想在插入数据时,id能够递增,而且这个ID开始值是我定的,我看了类式的IDENTITY ( data_type [ , seed , increment ] )这个不行,请大家帮忙。
例:插入ID为5开始值
5,'1'
6,'2'
7,'3'

解决方案 »

  1.   

    create table T(ID int identity(5,1),Name nvarchar(5))
      

  2.   

    create table a(ID int identity(5,1),Name nvarchar(5))insert a values('1')
    insert a values('2') 
    insert a values('3')select * from a--drop table a
    /*
    ID          Name
    ----------- -----
    5           1
    6           2
    7           3
    */
      

  3.   

    create table a(ID int identity(5,1),Name nvarchar(5))
    ----------
    这个就可以了,
    IDENTITY ( data_type [ , seed , increment ] )这个语法是在新增的时候使用的,
    定义表的看2楼风的.
    你说的这种是在如下情况使用,
    declare @tbA table (ID int identity(1,1),[Name] nvarchar(1))insert @tbA values('A')
    insert @tbA values('B') 
    insert @tbA values('C')
    --SELECT * FROM @tbA
    /*
    ID          Name
    ----------- ----
    1           A
    2           B
    3           C
    */
    SELECT id=identity(int,5,1), -- IDENTITY ( data_type [ , seed , increment ] )       
           [Name]
        INTO #temp
    FROM @tbA 
    select * from #temp
    /*
    id          Name
    ----------- ----
    5           A
    6           B
    7           C
    */