/*
例如传入的@code为'137',长度为@i为3,哪么code为'137'
加上@i再自动连续递增顺序,本人想过用'137'+replicate(0,@i)+'1',
但是不知如何连续递增,它在一个select语句中,又没法先查询和用
其它select先完成它,请教各位如何连续递增这个编号
*/
/*
我不清楚你用'137'+replicate(0,@i)+'1'是想变成1370001还是137001
假如是1370001
*/
declare @code varchar(10),@i int
select @code='137',@i=3
create table t(code varchar(10), name nvarchar(100))
insert t 
select '101005',N'彩三个五'
union all 
select '101002',N'三个五'
union all 
select '101008',N'纯三个五'
union all 
select '101009',N'10支红万'insert t 
select case when max(code) is NULL then @code+replicate(0,@i)+'1'
else convert(varchar,convert(int,max(code))+1) end ,''
from t where code like @code+'%' select * from t/*
code       name
---------- -----------------
101005     彩三个五
101002     三个五
101008     纯三个五
101009     10支红万
1370001    (5 行受影响)*/
drop table t

解决方案 »

  1.   

    楼主的意思没说明白.
    从前台传入@code,是否是指本条记录的code列的前三个字符为@code如'137'?
    后面的@i是指什么呢?你说长度为@i,可又要在'137'后面加上@i,又说要加@i个'0'再加'1',那不就是四位了吗?还有,在下面的再一次前台传入时,是否也有@code,是否到表中去找有没有与此相同的前三个字符去生成code?是否也还有@i?如果有,此@i起什么作用呢?
      

  2.   

    给你个参考:declare @newcode nvarchar(10),@code nvarchar(10)
    select @newcode=@code+right(1001+max(cast(right(code,3)as int)),3) from tb where left(code,3)=@code