2000的,求个触发器哦~例如一个销售订单,它分为表格外和表格内的两部分。
表格外的,例如有订单编号、客户名称、订购日期;
表格内的,例如有产品编号、产品名称、数量、单价等等,我还加了一个字段叫“行号”。每一个订单的表格内,当然可能有多行数据,我想在保存或修改这个订单时,自动从1开始生成行号。多谢各位了。

解决方案 »

  1.   

    表格外的,例如有订单编号、客户名称、订购日期;
    表格内的,例如有产品编号、产品名称、数量、单价等等,我还加了一个字段叫“行号”。create table tb1(订单编号 varchar(10),客户名称 varchar(10), 订购日期 varchar(10))create table tb2(订单编号 varchar(10),产品编号 varchar(10),数量 varchar(10),行号 int,id  int identity(1,1))
     alter trigger tr_t2  
     on tb2 for insert
    as 
    begin
     
    declare @row int
    select @row=max(行号) from tb2 a where exists(select 1 from  inserted  b where a.订单编号=b.订单编号)if @row is null select @row=1 else select @row=@row+1update tb2 set 行号=@row  from inserted a where a.订单编号=tb2.订单编号 and tb2.id=a.id --(增加id 的主键约束进行更新)
    end 
    insert into tb1 
    select '0001','a001','2010-08-05' union all
    select '0002','a002','2010-08-05' 
     insert into tb2(订单编号,产品编号,数量)select '0002','aa',100 
    insert into tb2(订单编号,产品编号,数量)select '0002','aa',100
    insert into tb2(订单编号,产品编号,数量)select '0002','aa',100 
    insert into tb2(订单编号,产品编号,数量)select '0001','aa',100 
    insert into tb2(订单编号,产品编号,数量)select '0001','aa',100 
    insert into tb2(订单编号,产品编号,数量) select '0001','aa',100 SELECT *FROM tb2 
    订单编号 产品编号 数量 行号 id
    0002   aa  100 1 13
    0002   aa  100  2 14
    0002   aa  100  3 15
    0001   aa  100  1 16
    0001   aa  100  2 17
    0001   aa  100  3 18虽然写了个触发器,但是还是建议你在前端程序做,相对要简单的很多。。
    小建议而已
      

  2.   

    可以在查询的时候使用row_number()来实现
      

  3.   

    是不是要这样的触发器?
    if object_id('tb') is not null drop table tb
    go
    create table tb(id int,ino_id int,gl_no int,inid int)
    go
    create trigger tri_tb on tb instead of insert 
    as 
    begin
        select *,rn1=identity(int,1,1) into #t1 from inserted
        select *,rn2=(select count(1) from #t1 where id=t.id and rn1<=t.rn1) into #t2 from #t1 t
        insert tb select i.id,i.ino_id,i.gl_no,isnull(max(t.inid),0)+i.rn2 
        from #t2 i left join tb t on t.id=i.id group by i.id,i.ino_id,i.gl_no,i.rn2
    end
    go
    insert tb 
    select 1,100,200,null union all
    select 1,100,200,null union all
    select 2,101,201,null union all
    select 3,102,202,null union all
    select 4,103,203,null 
    go
    insert tb 
    select 1,100,200,null union all
    select 1,100,200,null union all
    select 2,101,201,null union all
    select 3,102,202,null union all
    select 4,103,203,null 
    go
    select * from tb order by id,inid/*
    id          ino_id      gl_no       inid
    ----------- ----------- ----------- -----------
    1           100         200         1
    1           100         200         2
    1           100         200         3
    1           100         200         4
    2           101         201         1
    2           101         201         2
    3           102         202         1
    3           102         202         2
    4           103         203         1
    4           103         203         2(10 行受影响)*/
      

  4.   

    2000借助临时表
    2005借助row_number
      

  5.   

    谢谢大家的热心帮助。
    实际情况不允许用instead of触发器,所以,谢谢永生哥,辛苦了。我把杜安哥的触发器略微改了一下,加上了游标,不知道还有没有比游标更好的方式。alter trigger tr_t2  
     on tb2 for insert
    as 
    begin 
    declare @row INT
    DECLARE @ID INT
    SELECT RowNo=identity(int,1,1),id INTO #t1 FROM inserted
    DECLARE cur_insert CURSOR FOR SELECT * FROM #t1
    OPEN cur_insert
    FETCH NEXT FROM cur_insert INTO @row,@ID
    WHILE @@FETCH_STATUS=0
    BEGIN
    UPDATE tb2 SET 行号=@row WHERE id=@ID
    FETCH NEXT FROM cur_insert INTO @row,@ID
    END
    CLOSE cur_insert
    DEALLOCATE cur_insert
    end 
    go