表a,字段a_b,varchar型,存放数字字符串,如下:a_id   a_b
1      00077
2      00078
3      00076
4      00079
...插入新记录时,如何得到a_b字段的最大值,并自动加1后插入注意,a_b是VARCHAR型。谢谢

解决方案 »

  1.   

    declare @a table(a_id int, a_b varchar(10))
    insert @a select 1 ,'00077'
    union all select 2 ,'00078'
    union all select 3 ,'00076'
    union all select 4 ,'00079'
    select right('0000'+ltrim(cast(max(a_b) as int)+1),5) from @a
      

  2.   

    select right(('00000' + cast((cast(max(a_b) as int) + 1) as varchar(5))), 5)
      

  3.   

    insert into tableA(a_b) 
    select right(100000 + max(a_b) + 1,5) from tableA
      

  4.   

    忘了from 表 -_-#select right(('00000' + cast((cast(max(a_b) as int) + 1) as varchar(5))), 5)
    from a
      

  5.   

    create trigger a_insert on a
    for insert
    as 
    update a
    set a_b= select right('0000'+ltrim(cast(max(a_b) as int)+1),5) from a
    where a_id=@@identity
      

  6.   

    可能是我没说清楚,麻烦大家看我另一个请教贴,谢谢
    http://community.csdn.net/Expert/topic/5645/5645320.xml?temp=.3341181
      

  7.   

    declare @c varchar(10)select @c=right('0000'+cast((cast(max(a_b) as int)+1 as varchar),5) from table_Pqsinsert into table_Pqs(a_b) select @c