ABC/0001ABC/002为什么是002而不是0002?

解决方案 »

  1.   

    不好意思搞錯了是0002都是4位的如果開始沒有這個style就是從0001開始
      

  2.   

    create table 你的表 (ProductNo varchar(30),Style varchar(25))
    insert 你的表 values('ABC/0001',    'ABC')
    insert 你的表 values(null,    'ABC')
    insert 你的表 values(null,    'ABC')
    insert 你的表 values('AB/0002',    'AB')
    insert 你的表 values(null,    'AB')
    insert 你的表 values(null,    'AB')
    declare @a varchar(100),@b varchar(100),@i int
    update 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a
    where ProductNo is null
    select * from 你的表
    go
    drop table 你的表
      

  3.   

    上面是测试。你只要这样就可以了。
    declare @a varchar(100),@b varchar(100),@i intupdate 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a where ProductNo is null
      

  4.   

    思路,
    每插入一条记录,首先根据 Style 的值查询前面所有具有相同 Style 的记录,找出其中 ProductionNo 序号最大的,在此基础上加 1这些最好写在触发器里面,这样,不管你一次插入多少条记录,都可以。
      

  5.   

    回复人: cdshelf(cdshelf) ( ) 信誉:110  2003-08-30 10:37:00  得分:0 
      
      思路,
    每插入一条记录,首先根据 Style 的值查询前面所有具有相同 Style 的记录,找出其中 ProductionNo 序号最大的,在此基础上加 1这些最好写在触发器里面,这样,不管你一次插入多少条记录,都可以。
    那你就錯了
    一次插入多條只會觸發一次大力的語句我可是分析了一兩個小時才明白
    好蠢哦我
      
     
      

  6.   

    但是還沒有完全解決
    有一點小問題
    這是你的答案
    我稍微改了一下插入的順序create table 你的表 (ProductNo varchar(30),Style varchar(25))
    insert 你的表 values(null,    'ABC')
    insert 你的表 values(null,    'AB')
    insert 你的表 values(null,    'ABC')
    insert 你的表 values(null,    'AB')
    insert 你的表 values(null,    'ABC')
    declare @a varchar(100),@b varchar(100),@i int
    update 你的表 set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from 你的表 tem where tem.style=你的表.style),0)+@i,4),@b=style,productno=@a
    where ProductNo is null
    select * from 你的表
    go
    drop table 你的表和插入順序有關
    也和又沒有productNo種子有關這樣再看看
    insert 你的表 values('ABC/0001',    'ABC')
    insert 你的表 values('AB/0001',    'AB')
    insert 你的表 values(null,    'ABC')
    insert 你的表 values(null,    'AB')
    insert 你的表 values(null,    'ABC')
    萬望百忙之中再次關注一下這個問題
    謝謝
      

  7.   

    沒有的本來productId可以是主鍵但是我在插數據進去的時候
    productno都是null還有isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) 
    這一句有點問題
    應該改成isnull((select right(max(ProductNo),4)
    要不然ProductNo太長的時候就有問題了.
      

  8.   

    create table #a (id int identity(1,1),ProductNo varchar(30),Style varchar(25))
    insert #a (ProductNo,Style) values(null,    'ABC')
    insert #a (ProductNo,Style) values(null,    'AB')
    insert #a (ProductNo,Style) values(null,    'ABC')
    insert #a (ProductNo,Style) values(null,    'AB')
    insert #a (ProductNo,Style) values(null,    'ABC')select * into # from #a where ProductNo is null order by style,id
    declare @a varchar(100),@b varchar(100),@i int
    update # set @i=case when @b=style then @i+1 else 1 end,@a=style+'/'+right(10000+isnull((select right(max(ProductNo),charindex('/',max(ProductNo))-1) from # tem where tem.style=#.style),0)+@i,4),@b=style,productno=@aupdate #a set productno=#.productno from # where #a.id=#.idselect * from #a
    go
    drop table #a,#