我想写个存储过程插入一条新记录并返回新记录的自动增长的ID号怎么搞的总是返回1

解决方案 »

  1.   

    trySelect IDENT_CURRENT('table_name')
      

  2.   

    详解  @@identity我为什么总是返回的1 ?
      

  3.   

    create table ta(id int identity(1,1),name varchar(5))
    create proc test @name varchar(2),@i int output
    as
    insert ta
    select @name
    return @@identityexec test 'aa'
      

  4.   

    create table test(id int identity(1,1),code varchar(20))
    gocreate procedure sp_test(@code varchar(20),@id int output)
    as
    begin
        insert into test values(@code)
        select @id=@@identity    return
    end
    godeclare @id intexec sp_test 'AAAA',@id output
    select @id
    exec sp_test 'BBBB',@id output
    select @id
    exec sp_test 'CCCC',@id output
    select @idselect * from test
    godrop procedure sp_test
    drop table test 
    go
      

  5.   

    create table test(id int identity(1,1),code varchar(20))
    gocreate procedure sp_test(@code varchar(20),@id int output)
    as
    begin
        insert into test values(@code)
        select @id=@@identity    return
    end
    godeclare @id intexec sp_test 'AAAA',@id output
    select @id
    /*
    ----------- 
    1
    */exec sp_test 'BBBB',@id output
    select @id
    /*
    ----------- 
    2
    */exec sp_test 'CCCC',@id output
    select @id
    /*
    ----------- 
    3
    */select * from test
    /*
    id          code                 
    ----------- -------------------- 
    1           AAAA
    2           BBBB
    3           CCCC
    */
    godrop procedure sp_test
    drop table test 
    go
      

  6.   

    create table ta(id int identity(1,1),name varchar(5))alter proc test @name varchar(2),@i int output
    as
    insert ta values( @name)
    select @@identity
    declare @i int
    exec test 'bb',@i output
      

  7.   

    SELECT  @ID = IDENT_CURRENT('表名')
    用这个