--示例:--创建测试的表
create table 表(流水号 char(14) default '',姓名 varchar(10),其它 int)
go--创建处理的触发器
create trigger tr_insert on 表
instead of insert
as
select * into #t from inserted order by 姓名declare @dt char(8),@姓名 varchar(10),@i int
set @dt=convert(char(10),getdate(),112)
select @i=isnull(cast(right(max(流水号),6) as bigint),0)
from 表
where 流水号 like @dt+'%'update 表 set 流水号=b.流水号
from #t a join(
select 姓名,流水号=max(流水号)
from 表 where 流水号 like @dt+'%'
group by 姓名
)b on a.姓名=b.姓名update #t set @i=case 姓名 when @姓名 then @i else @i+1 end
,流水号=@dt+right('000000'+cast(@i as varchar),6)
,@姓名=姓名
where isnull(流水号,'')=''insert 表 select * from #t
go--插入数据测试
insert 表(姓名,其它)
select 'aa',123
union all select 'bb',234
union all select 'aa',1234
union all select 'bb',1234insert 表(姓名,其它)
select 'cc',123
go--显示处理结果
select * from 表
go--删除测试
drop table 表/*--测试结果
流水号            姓名         其它          
-------------- ---------- ----------- 
20040403000001 aa         123
20040403000001 aa         1234
20040403000002 bb         1234
20040403000002 bb         234
20040403000003 cc         123(所影响的行数为 5 行)
--*/

解决方案 »

  1.   

    --当然啦,我的不支持自增列.要支持自增列的话,要另行处理.
    --下面是示例:--创建测试的表
    create table 表(id int identity(1,1),流水号 char(14) default '',姓名 varchar(10),其它 int)
    go--创建处理的触发器
    create trigger tr_insert on 表
    instead of insert
    as
    select 流水号,姓名,其它  --这里就一定要写上除自增列外的字段名
    into #t from inserted order by 姓名declare @dt char(8),@姓名 varchar(10),@i int
    set @dt=convert(char(10),getdate(),112)
    select @i=isnull(cast(right(max(流水号),6) as bigint),0)
    from 表
    where 流水号 like @dt+'%'update 表 set 流水号=b.流水号
    from #t a join(
    select 姓名,流水号=max(流水号)
    from 表 where 流水号 like @dt+'%'
    group by 姓名
    )b on a.姓名=b.姓名update #t set @i=case 姓名 when @姓名 then @i else @i+1 end
    ,流水号=@dt+right('000000'+cast(@i as varchar),6)
    ,@姓名=姓名
    where isnull(流水号,'')=''insert 表 select * from #t
    go--插入数据测试
    insert 表(姓名,其它)
    select 'aa',123
    union all select 'bb',234
    union all select 'aa',1234
    union all select 'bb',1234insert 表(姓名,其它)
    select 'cc',123
    go--显示处理结果
    select * from 表
    go--删除测试
    drop table 表/*--测试结果
    流水号            姓名         其它          
    -------------- ---------- ----------- 
    20040403000001 aa         123
    20040403000001 aa         1234
    20040403000002 bb         1234
    20040403000002 bb         234
    20040403000003 cc         123(所影响的行数为 5 行)
    --*/
      

  2.   

    姓名连续插入两个ASD看看。有问题的啊
      

  3.   

    为什么我的显示结果是:20040403000001 aa         123
    20040403000001 aa         1234
    20040403000001 bb         1234
    20040403000001 bb         234
    20040403000001 cc         123???
      

  4.   

    执行你的上述语句以后再执行select * from 表表就可以看到问题了。
      

  5.   

    --可以了,原来写错表名
    --下面是示例:--创建测试的表
    create table 表(id int identity(1,1),流水号 char(14) default '',姓名 varchar(10),其它 int)
    go--创建处理的触发器
    create trigger tr_insert on 表
    instead of insert
    as
    select 流水号,姓名,其它  --这里就一定要写上除自增列外的字段名
    into #t from inserted order by 姓名declare @dt char(8),@姓名 varchar(10),@i int
    set @dt=convert(char(8),getdate(),112)
    select @i=isnull(cast(right(max(流水号),6) as bigint),0)
    from 表
    where 流水号 like @dt+'%'update #t set 流水号=b.流水号
    from #t a join(
    select 姓名,流水号=max(流水号)
    from 表 where 流水号 like @dt+'%'
    group by 姓名
    )b on a.姓名=b.姓名update #t set @i=case 姓名 when @姓名 then @i else @i+1 end
    ,流水号=@dt+right('000000'+cast(@i as varchar),6)
    ,@姓名=姓名
    where isnull(流水号,'')=''insert 表 select * from #t
    go--插入数据测试
    insert 表(姓名,其它)
    select 'aa',123
    union all select 'bb',234
    union all select 'aa',1234
    union all select 'bb',1234insert 表(姓名,其它)
    select 'cc',123
    union all select 'bb',1234
    union all select 'bb',1234
    union all select 'bb',1234
    union all select 'bb',1234go--显示处理结果
    select * from 表
    go--删除测试
    drop table 表/*--测试结果
    id          流水号            姓名         其它          
    ----------- -------------- ---------- ----------- 
    1           20040403000001 aa         123
    2           20040403000001 aa         1234
    3           20040403000002 bb         1234
    4           20040403000002 bb         234
    5           20040403000002 bb         1234
    6           20040403000002 bb         1234
    7           20040403000002 bb         1234
    8           20040403000002 bb         1234
    9           20040403000003 cc         123(所影响的行数为 9 行)
    --*/
      

  6.   

    --可以了,原来写错表名
    --下面是示例:--创建测试的表
    create table 表(id int identity(1,1),流水号 char(14) default '',姓名 varchar(10),其它 int)
    go--创建生成流水号的存储过程
    create proc p_process
    as
    declare @dt char(8),@i bigint
    set @dt=convert(char(8),getdate(),112)--更新已经有编号的姓名
    --特别要求,将流水号的默认值设置成'',因为这样处理比处理NULL速度快
    update 表 set 流水号=b.流水号
    from 表 a join(
    select 姓名,流水号=max(流水号)
    from 表 where 流水号 like @dt+'%'
    group by 姓名
    )b on a.姓名=b.姓名
    where a.流水号='' --如果要求能处理NULL的情况,则用:isnull(流水号,'')=''--处理没有编号的姓名
    select id=identity(int,1,1),姓名 into #t
    from 表 
    where 流水号='' --如果要求能处理NULL的情况,则用:isnull(流水号,'')=''
    group by 姓名select @i=cast(right(max(流水号),6) as bigint)
    from 表
    where 流水号 like @dt+'%'if @i is null set @i=0update 表 set 流水号=@dt+right('000000'+cast(b.id+@i as varchar),6)
    from 表 a join #t b on a.姓名=b.姓名
    where 流水号=''
    go
    --插入数据测试
    insert 表(姓名,其它)
    select 'aa',123
    union all select 'bb',234
    union all select 'aa',1234
    union all select 'bb',1234--生成流水号
    exec p_process
    goinsert 表(姓名,其它)
    select 'cc',123
    union all select 'bb',1234
    union all select 'bb',1234
    union all select 'bb',1234
    union all select 'bb',1234--生成流水号
    exec p_process
    go--显示处理结果
    select * from 表
    go--删除测试
    drop table 表
    drop proc p_process/*--测试结果
    id          流水号            姓名         其它          
    ----------- -------------- ---------- ----------- 
    1           20040403000001 aa         123
    2           20040403000002 bb         234
    3           20040403000001 aa         1234
    4           20040403000002 bb         1234
    5           20040403000003 cc         123
    6           20040403000002 bb         1234
    7           20040403000002 bb         1234
    8           20040403000002 bb         1234
    9           20040403000002 bb         1234(所影响的行数为 9 行)
    --*/