create table ap(name1 char(10),code char(3))
insert ap
select '广州','001' union all
select '北京','002'
select right(1000+isnull(cast(max(code) as int)+1,1),3) from ap
insert ap
select '广州','001' union all
select '北京','002'
select right(1000+isnull(cast(max(code) as int)+1,1),3) from ap
select REPLICATE('0',3-len(10))+str(10,len(10))
注释:
REPLICATE函数:以指定的次数重复字符表达式,在例子里如果长度不足时在前面填充字符"0"
3:是字符的总长度,如果字符长度小于这个数时,用"0"来填充
3-len(10):计算出字符不足的位数
10:是需要转换的字符
str(10,len(10)):将数值型转换成字符型,如果不加len(10),转换过到字符会有多余空格