select rg+right('000'+cast(cast(right((select max(djh) from aa where substring(djh,2,8)=b.rq),3) as int)+cast(lb as int) as varchar(10)),3) as djh
,* from bb b

解决方案 »

  1.   

    create function f_gen_id(@rq varchar(8))
    returns varchar(12)
    as 
    begin 
    declare @djh varchar(12)
    select @djh=isnull(max(djh),'L'+@rq+'000') from aa where substring(djh,2,8)=@rq
    declare @id varchar(3)
    select @id=stuff(@djh,1,9,'')
    select @id=cast( cast(@id as int)+1 as varchar(3))
    set @djh=substring(@djh,1,9)+replicate('0',3-len(@id))+@id
    return(@djh)
    end
      

  2.   

    两个表没有关联,我就是想利用aa表中当天的最大单据号,利用SELECT语句形成
       djh              rq       lb 
       L20040808005    20040808  01
       L20040808006    20040808  02
       L20040808007    20040808  03
    这个查询结果
      

  3.   

    create table aa(djh nvarchar(100))
    insert into aa select 'L20040808001'
    union all select    'L20040808002'
    union all select    'L20040808003'
    union all select    'L20040808004'
    union all select    'L20040809001'
    union all select    'L20040809002'
    union all select    'L20040809003'
    union all select    'L20040809004'
    create table  bb(rq nvarchar(8),lb nvarchar(2)) 
    insert into bb select  '20040808' ,  '01'
    union all select    '20040808'   ,'02'
    union all select    '20040808'   ,'03' 
    union all select    '20040809'   ,'01'
    union all select    '20040809'   ,'02' 
    select rq+right('000' + convert(nvarchar,convert(int,rdjh)+convert(int,lb)),3) as djh,rq,lb
    from bb L  join (select left(djh,9) as ldjh,max(right(djh,3)) as rdjh from aa group by left(djh,9)) R
    on L.rq=right(R.ldjh,8)drop table aa,bb/*
    20040808005 20040808 01
    20040808006 20040808 02
    20040808007 20040808 03
    20040809005 20040809 01
    20040809006 20040809 02*/
      

  4.   

    select 'L' + convert( varchar(20), lb + cast( (select max( right(djh,len(djh)-1) ) from aa) as bigint ) ) as djh, rq, lb from bb
      

  5.   

    如果没有关联
     create table aa(djh nvarchar(100))
    insert into aa select 'L20040808001'
    union all select    'L20040808002'
    union all select    'L20040808003'
    union all select    'L20040808004'
    union all select    'L20040809001'
    union all select    'L20040809002'
    union all select    'L20040809003'
    union all select    'L20040809004'
    create table  bb(rq nvarchar(8),lb nvarchar(2)) 
    insert into bb select  '20040808' ,  '01'
    union all select    '20040808'   ,'02'
    union all select    '20040808'   ,'03' 
    declare @djh nvarchar(100)
    select @djh=max(djh) from aa 
    select left(@djh,9)+right('000' + convert(nvarchar,convert(int,right(@djh,3))+convert(int,lb)),3) as djh,rq,lb
    from bbdrop table aa,bb/*
    L20040809005 20040808 01
    L20040809006 20040808 02
    L20040809007 20040808 03*/
      

  6.   

    pisces007(蝶鱼) 你好
    我可能没说明白你是把lb用上了,如果lb不是01,02,03 而是aa ,bb cc 之类的呢。
      

  7.   

    就象下面的格式
      rq         lb 
       20040808   aa
       20040808   bb
       20040808   ll 
      

  8.   

    create table aa(djh nvarchar(100))
    insert into aa select 'L20040808001'
    union all select    'L20040808002'
    union all select    'L20040808003'
    union all select    'L20040808004'create table  bb(rq nvarchar(8),lb nvarchar(2)) 
    insert into bb select  '20040808' ,  'aa'
    union all select    '20040808'   ,'bb'
    union all select    '20040808'   ,'cc' 
    declare @djh nvarchar(100)
    select @djh=max(djh) from aa 
    select identity(int,1,1) as id,* into #a from bbselect left(@djh,9)+right('000' + convert(nvarchar,convert(int,right(@djh,3))+id),3) as djh,rq,lb
    from #a
    drop table #a
    drop table aa,bb/*
    L20040808005 20040808 aa
    L20040808006 20040808 bb
    L20040808007 20040808 cc*/
      

  9.   

    create table  aa(djh varchar(50))
    insert aa select 'L20040808001'
    insert aa select 'L20040808002'
    insert aa select 'L20040808003'
    insert aa select 'L20040808004'
    go
    create table bb(rq varchar(50),lb varchar(50))
    insert bb select '20040808','01'
    insert bb select '20040808','02'
    insert bb select '20040808','03'
    go
    create table cc (djh varchar(50),rq varchar(50),lb varchar(50))
    go
    drop function CreateSer
    go
    create function CreateSer ()---创建取得编号的函数
    returns varchar(50)
    as
    begin
       declare @nn int
       declare @Ser varchar(50)
       declare @Tp  varchar(50)
       select @nn=count(*) from cc --检查cc表中是否已经存在纪录
      if @nn>0 
            begin
    select @Tp=cast((right(max(djh),3)+1) as int) from cc
    select @Ser=left(djh,9)+ right('000'+ @tp,3)  from cc
        end 
         else 
    begin
    select @Tp=cast((right(max(djh),3)+1) as int) from aa
    select @Ser=left(djh,9)+ right('000'+@tp,3) from aa
    end
        
       return @Ser
    end
    goinsert cc select dbo.CreateSer(),* from bb
    select * from cc
      

  10.   


    蝶鱼你好:工作中又遇到一个问题,就是把lb也放入djh中,是否能够实现如下的显示,多谢。
       
    现在一个表  aa    
         djh(单据号)   
         Laa20040808001  
         Laa20040808002  
         Lbb20040808003  
         Lcc20040808005  
         Lcc20040808006  
         Lcc20040808007  另一张表  bb    
         rq                  lb    
         20040808      aa  
         20040808      bb  
         20040808      cc    
    想利用bb表的rq与lb生成一个djh应为  
         djh                            rq              lb    
         Laa20040808003        20040808    01  
         Lbb20040808004        20040808    02  
         Lcc20040808008        20040808    03
      

  11.   

    create table aa(djh nvarchar(100))
    insert into aa select 'Laa20040808001'
    union all select    'Laa20040808002'
    union all select    'Lbb20040808003'
    union all select    'Lcc20040808005'
    union all select    'Lcc20040808006'
    union all select    'Lcc20040808007'create table  bb(rq nvarchar(8),lb nvarchar(2)) 
    insert into bb select  '20040808' ,  'aa'
    union all select    '20040808'   ,'bb'
    union all select    '20040808'   ,'cc' select R.ldjh+right('000' + convert(nvarchar,convert(int,R.rdjh)+1),3) as djh,L.rq,L.lb
    from bb L  join (select substring(djh,2,2) as lb,max(left(djh,11)) as ldjh,max(right(djh,3)) as rdjh from aa group by  substring(djh,2,2)) R
    on L.lb=R.lbdrop table aa,bb/*
    Laa20040808003 20040808 aa
    Lbb20040808004 20040808 bb
    Lcc20040808008 20040808 cc
    */