用case判断一下不可以吗?然后获取序号,再格式化为你要的格式

解决方案 »

  1.   

    在存储过程中用case when判断传进来的参数,然后对应返回一个值.
      

  2.   

    create table tb (name varchar(10), code varchar(10) , px varchar(10))
    gocreate procedure my_proc @name varchar(10)
    as
    begin
      declare @px as varchar(10)
      set @px = ''
      if @name = '全棉' 
         begin
           select @px = max(px) from tb where code = 'C'
           if @px is null 
              insert into tb values(@name , 'C' , '000001')
           else
              insert into tb values(@name , 'C' , right('000000'+cast(cast(@px as int)+1 as varchar),6))
         end
      if @name = '全涤' 
         begin
           select @px = max(px) from tb where code = 'T'
           if @px is null 
              insert into tb values(@name , 'T' , '000001')
           else
              insert into tb values(@name , 'T' , right('000000'+cast(cast(@px as int)+1 as varchar),6))
         end
    end
    goexec my_proc '全棉' 
    exec my_proc '全棉' 
    exec my_proc '全棉' 
    exec my_proc '全涤' 
    exec my_proc '全涤' select * from tbdrop table tb
    drop procedure my_proc/*
    name       code       px         
    ---------- ---------- ---------- 
    全棉         C          000001
    全棉         C          000002
    全棉         C          000003
    全涤         T          000001
    全涤         T          000002(所影响的行数为 5 行)
    */
      

  3.   

    刚才去开会了。。
    我只做了一个例子,表那个地方你可以把它删除掉,换成你需要的表。/*************** 
    @falg  :
    1,全棉
    2,全涤
    3,人棉
    4,涤棉
    5,涤人棉
    exec TB_test 1
    ***************/
    alter proc TB_test
    @falg int
    as
    set nocount on
    --测试表
    declare @table table
    (
    formatname nvarchar(40),
    formatnum nvarchar(100)
    )
    declare @count int
    if @falg=1
    begin
    set @count=(select count(*)from @table where formatname='全棉')+1
    insert @table values('全棉',left('T000000',7-len(@count))+cast(@count as nvarchar(10)))
    select * from @table
    end
      

  4.   

    alter  改成  create再运行
    然后单步运行 exec TB_test 1