在insert时,能够获得要插入行的自增编号,就是在插入时就能获得自增编号。
现在我是用两条语句来实现的,先插入空行,在update行,用了一个过程。
现在希望能一条语句实现,盼望指教,谢谢。

解决方案 »

  1.   


    @@identity
    贴出你写的来看看
      

  2.   

    建表时
    create table tb(id int identity,......)
      

  3.   

    INSERT 语句;                                                  SELECT SCOPE_IDENTITY()
      

  4.   

    --@@IDENTITY和SCOPE_IDENTITY和IDENT_CURRENT的区别
    @@IDENTITY--是得到当前会话的所有范围的最后插入的IDENTITY值 
    SCOPE_IDENTITY--是得到当前会话的当前范围的最后插入的IDENTITY值 
    IDENT_CURRENT--是得到指定表的最后插入的IDENTITY值,与会话、范围无关。
      

  5.   

    写到一起如下格式:
    Insert 语句; SELECT SCOPE_IDENTITY() 就可以了得到自增ID
      

  6.   

    insert tablename(col1,col2) select @@identity 
      

  7.   

    select max(自增编号)+1 ...
      

  8.   

    max到时候是得到最大的数值,如果我插入到了第25行,我又删除了第25行,我再插入时我得到的自增编号是26,但max得到是25。
    我是想在插入前得到自增编号,同时写入到插入的插入数据的某一列中。
      

  9.   

    我也认为是MAX(ID) +1 直接从表中取
      

  10.   

    好像是要实现自动填补断号的功能?如果用MAX(ID) +1也不对,如果删除当中的一天记录就不管用了。建议要写个整理断号的功能,顺序改变ID号码,不要设置成自增量了。
    例:
    1、2、3、4、5
    删除3

    1、2、4、5
    删除3之后调用整理功能改为
    1、2、3、4
    然后添加时就用到5了。
      

  11.   

    create table # (autoid int identity(1,1),f2 varchar(20) )insert into #
    select 'aaaaaaa' union all 
    select 'bbbbbbbbbb' union all
    select 'ccccc' union all 
    select 'dddddddd' union all
    select 'eeeeeeeee' union all 
    select 'fffffffffff' union all
    select 'ggggggggg' union all 
    select 'hhhhhhhhh' union all
    select 'iiiiiiiii' union all 
    select 'jjjjjj' union all
    select 'kkkkk' union all 
    select 'lllllll' union all
    select 'mmmmmmmm' union all 
    select 'nnnnnnnn' union all
    select 'oooooo' union all 
    select 'ppppp' union all
    select 'qqqqqqq' union all 
    select 'rrrrrr' --这一句可以显示
    select @@identity
      

  12.   

    参考:
    Use test
    Go
    If object_id('c_test') Is Not Null
    Drop Proc c_testIf object_id('test') Is Not Null
    Drop Table test
    Go
    Create Table test(id int Identity(1,1) Primary Key, x int)
    Go
    Create Proc c_test
    (
    @id int Output,
    @x int
    )
    As
    Insert Into Test (x) Values (@x)
    Set @id=Scope_identity()
    Go
    Declare @newid int
    Exec c_test @newid Output,525 Select @newid As [刚Insert的ID]/*
    刚Insert的ID
    -----------
    1
    */
      

  13.   

    参考:
    Use test
    Go
    If object_id('c_test') Is Not Null
    Drop Proc c_testIf object_id('test') Is Not Null
    Drop Table test
    Go
    Create Table test(id int Identity(1,1) Primary Key, x int)
    Go
    Create Proc c_test
    (
    @id int Output,
    @x int
    )
    As
    Insert Into Test (x) Values (@x)
    Set @id=Scope_identity()
    Go
    Declare @newid int
    Exec c_test @newid Output,525 Select @newid As [刚Insert的ID]/*
    刚Insert的ID
    -----------
    1
    */