日期:2014-05-18  浏览次数:20633 次

看一下存储过程~
SQL code

CREATE TABLE test
(
    id int NOT NULL PRIMARY KEY identity(1,1),
    OrderNumber nvarchar(50) 
)
insert into test values('2012HJLS1000000001')


if(exists(select * from sysobjects where name='GetCode' ))
drop proc GetCode
go

create proc GetCode
    @shuliang int
as
    declare @i int
    declare @OrderNumber nvarchar(50)
    declare @bianliang nvarchar(50)
    set @i = 0 
    while @i < @shuliang 
    begin
      select top 1 @OrderNumber=OrderNumber from test order by id desc
      set @bianliang=right(cast(right(@OrderNumber,10)+0000000001 as varchar),10)
      insert into test values('2012HJLS'+@bianliang)
      set @i = @i + 1 
    end 
go

exec GetCode 10



问题描述:right(cast(right('2012HJLS1000000001',10)+0000000001 as varchar),10)
这样执行就会生成一样 1000000002 这样的一个卡号~现在我不是想从1000000001开始 而是想从000000001开始,请问怎么实现呢??

------解决方案--------------------
set @bianliang=right('0000000000'+cast(right(@OrderNumber,10)+1 as varchar(20)),10)