change your logic, use an identity column, after the user information is inserted into the database, retrieve the value, and return it to the user

解决方案 »

  1.   

    1、建立一个“序数表”
    create table 序数表(表名 varchar(1000),序号 bigint)2、建立一个过程:
    CREATE proc 得到序号
    @表 varchar(1000),
    @结果 bigint output
    as
    ---如果是第一次,添入初始值
    insert 序数表 select @表名,1 where not exists(select 1 from 序数表 where 表名=@表名)
    begin tran
      select @结果=序号 from 序数表 where 表名=@表名
        if @@error<>0 goto error
    --得到后马上加一保持序号的完全唯一
      update 序数表 set 序号=序号+1 where 表名=@表名
        if @@error<>0 goto error
    commit tran
    return
    error:
      rollback tran
    GO3、在前台调用:
    declare @a bigint
    exec 得到序号 '我的表',@a output
    select @a