大批数据插入,在插入触发器里不能这样写:select @code=code,@crrq=rkrq,@dj=price,@crdh=rkdh,
          @kcsl=in_number,@csid=factory_id,@rksl=in_number,@fph=fph
from inserted
===>这样只是获得插入的最后一条记录,应该采取联合
insert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)values
(@id,@code,@crrq,@crdh,@dj,@rksl,@kcsl,@dj*@kcsl,1)===>
insert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)values
select @id,code,crrq,crdh,price,crsl,kcsl,kcje, from inserted

解决方案 »

  1.   

    CREATE trigger tr_in_device on device_in1 for insert
    as
    declare @id int,
    @code int,
    @crrq datetime,
    @crdh int,
    @dj money,
    @kcsl int,
             @csid int,
             @rksl int,
             @fph varchar(18),
             @crsl int,
             @crlx int,
             @jxsid int,
             @kccount int,
             @ccrq datetime,
             @ccsn varchar(24)--将记录插入临时表
    select *,identity(int,1,1) 'xuhao' into # from inserteddeclare @i int
    set @i=1--循环
    while @i<=(select max(xuhao) from #)
    beginselect @id=max(obid)+1 from sysid where lx=3
    select @code=code,@crrq=rkrq,@dj=price,@crdh=rkdh,
              @kcsl=in_number,@csid=factory_id,@rksl=in_number,@fph=fph
    from # where xuhao=@i
    select @kcsl=@kcsl+kcsl 
    from device where  =1 and code=@code
    set @kccount=(select count(device_id) from device where =1 and code=@code)select @crlx=crlx,@jxsid=jxsbm from device_in0 where rkdh=@crdh
    select @ccrq=ccrq,@ccsn=ccsn from device_code where code=@codeif(@kccount>0)begin
       update device set =0 where  =1 and code=@code
    endinsert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)values
    (@id,@code,@crrq,@crdh,@dj,@rksl,@kcsl,@dj*@kcsl,1)if(@dj>=500)begin
       insert into rkdj(crdh,code,crlx,jxsid,factoryid,rkrq,dj,rksl,rkje,,fph,ccrq,ccsn) values
       (@crdh,@code,@crlx,@jxsid,@csid,@crrq,@dj,@rksl,@dj*@rksl,0,@fph,@ccrq,@ccsn)
    endupdate sysid set obid=@id where lx=3set @i=@i+1
    enddrop table #
      

  2.   

    insert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)values
    select @id,code,crrq,crdh,price,crsl,kcsl,kcje, from inserted=======>上面写错了
    insert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)
    select @id,code,crrq,crdh,price,crsl,kcsl,kcje, from inserted
      

  3.   

    我看我只能用循环好一点.用下面的语句
    insert into device(device_id,code,crrq,crdh,price,crsl,kcsl,kcje,)
    select @id,code,crrq,crdh,price,crsl,kcsl,kcje, from inserted
    好是好,但是kcsl是下面的情形:
    select @kcsl=@kcsl+kcsl 
    from device where  =1 and code=@code
      

  4.   

    大批数据插入,在插入触发器里不能这样写:
      select @code=code ..... from inserted  这样只是获得插入的最后一条记录,应该采取联合
    这是经常会犯的一个错误,很多人都会找不到原因,其实你可以用@rowcount看看每次处理了多少条记录,在触发器的第一条记录 测试一下@rowcount的值,就很明白了。
    最近也有类似的贴子。你可以查查看。了解一下更多的原因和做法。具体写法看上面的两位就ok了。