select max(id)+1 from tablename where username=@username

解决方案 »

  1.   

    select count(*)+1 into a from table where username=@username
      

  2.   

    select @id= max(id) from tablea
    insert into tablea select @id,@usename,@access
      

  3.   


    就是先取最打同一个username的最大id,然后加1再插入,或者先插入再更新。
      

  4.   

    declare @id int ,@username varchar(20),@access varchar(50)
    select @id = count(*)+1 from TableA  
    insert into TableA
    values 
    (@id ,
     @username,
     @access
    )
      

  5.   

    不知你是否考虑到:当删除一个记录(非最后一条)后,再插入一个记录时,这时新插入的记录的ID 和原来最后一条记录的ID 是相同的!就失去了ID 也是主键的意义(“id 主键,不是自动编号”)eg:
    id    username   access
    ---------------------------------
    1     ni         c:\win\
    2     ss         c:\win\a
    3     pp         d:\ (注意:)当删除 id = 2  后再添加一记录时:
    id    username   access
    ---------------------------------
    1     ni         c:\win\
    3     pp         d:\
    3     qq         c:\
      

  6.   

    回复人: yangh44(涉世之初) ( ),说的有道理