当数据库批量增加时,触发器不执行?

huobao 2002-06-10 08:42:58
当向数据库单个增加数据时,触发器正确执行;而当 insert .... (select...) 时,触发器就不好用了,这是为什么?

触发器的功能是:当该表增加一行时,取其中的销量,来减少库存。
...全文
102 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
alandd 2002-06-10
  • 打赏
  • 举报
回复
很简单,你的触发器是行级触发器,只有单行时才会触发
圣殿骑士18 2002-06-10
  • 打赏
  • 举报
回复
呵呵
WorldMobile 2002-06-10
  • 打赏
  • 举报
回复
一楼的这么快,等我敲了这么多汉字后,你的代码都粘上了
WorldMobile 2002-06-10
  • 打赏
  • 举报
回复
我知道你的问题所在,你的触发器只处理了一行数据即
向数据库增加一条数据时没有问题,但如果同时插入多条数据时
触发器不是不好用,是它只处理了一条或者一条也没有处理
如果要在程序中处理同时插入多条数据时的问题,
你触发器中必须用游标来处理即你定义
或者你不允许多条数据同时插入,
不要问我你在PB的数据窗口中插入多条数据怎么没有问题,
那是因为PB的数据窗口的存盘是逐行处理的
不信,你可以测试测试数据窗口提交的SQL语句
你有两个方法
1.用游标来处理插入的数据
2.不允许同时插入多行数据
圣殿骑士18 2002-06-10
  • 打赏
  • 举报
回复
你触发器写的不对,我估计你的方法只能取得inserted表其中的一行,漏了其他行
必须用游标。
看看我的代码:

alter trigger DBA.tr_klomain_u_billstate
on DBA.klomain for update
as
if update(billstate) begin --1
--关于库存
declare @whcode_i char(10),@whposcode_i char(8),@billstate_i integer,
@whcode_d char(10),@whposcode_d char(8),@billstate_d integer,
@invcode_i char(20),@batch_i char(10),@packcode_i char(2),@unitcode_i char(2),
@invcode_d char(20),@batch_d char(10),@packcode_d char(2),@unitcode_d char(2),
@packqty_i integer,@singleqty_i numeric(8,2),@totalqty_i numeric(12,2),
@packqty_d integer,@singleqty_d numeric(8,2),@totalqty_d numeric(12,2),
@totalqty_wh numeric(12,2),@myrow integer,
--
@id_i numeric(10),@billid_i char(15),@fptype_i char(20),
@sumprice_i numeric(15,2),
@sumprice_jy numeric(15,2),@billdate_i timestamp
--end
declare cur_dxf dynamic scroll cursor for select I.id,I.billid,I.billdate,
I.sumprice,
I.invcode,I.batch,I.packcode,I.unitcode,I.packqty,
I.singleqty,I.totalqty,I.whcode,I.billstate,
D.invcode,D.batch,D.packcode,D.unitcode,D.packqty,
D.singleqty,D.totalqty,D.whcode,D.billstate from
deleted as D,inserted as I where
I.id=D.id
open cur_dxf
fetch next cur_dxf into @id_i,@billid_i,@billdate_i,
@sumprice_i,
@invcode_i,@batch_i,@packcode_i,@unitcode_i,@packqty_i,
@singleqty_i,@totalqty_i,@whcode_i,@billstate_i,
@invcode_d,@batch_d,@packcode_d,@unitcode_d,@packqty_d,
@singleqty_d,@totalqty_d,@whcode_d,
@billstate_d
while @@sqlstatus=0
begin --2
--uncheck in
if @billstate_d=1 and @billstate_i=0
begin
--同步库存
select @totalqty_wh=totalqty from
wh_kc where
whcode=@whcode_d
and invcode=@invcode_d
and ifnull(batch,'',batch)=ifnull(@batch_d,'',@batch_d)
and unitcode=@unitcode_d
select @myrow=@@rowcount
if @myrow>1
raiserror 100002 '|库存中此货品的多条记录,会导致误操作,不能保存!|'
if @myrow=0
insert into wh_kc(whcode,invcode,batch,unitcode,totalqty) values(
@whcode_d,@invcode_d,@batch_d,@unitcode_d,@totalqty_d)
else
update wh_kc set
totalqty=totalqty+@totalqty_d where
whcode=@whcode_d
and invcode=@invcode_d
and ifnull(batch,'',batch)=ifnull(@batch_d,'',@batch_d)
and unitcode=@unitcode_d
end
--check out
if @billstate_d=0 and @billstate_i=1
begin
--同步库存
select @totalqty_wh=totalqty from
wh_kc where
whcode=@whcode_i
and invcode=@invcode_i
and ifnull(batch,'',batch)=ifnull(@batch_i,'',@batch_i)
and unitcode=@unitcode_i
select @myrow=@@rowcount
if @myrow=0
raiserror 100001 '|库存中没有此货品,不能保存!|'
if @myrow>1
raiserror 100002 '|库存中此货品的多条记录,会导致误操作,不能保存!|'
if @totalqty_wh<@totalqty_i
raiserror 100003 '|库存中没有足够的此货品,不能保存!|'
update wh_kc set
totalqty=totalqty-@totalqty_i where
whcode=@whcode_i
and invcode=@invcode_i
and ifnull(batch,'',batch)=ifnull(@batch_i,'',@batch_i)
and unitcode=@unitcode_i
end
--4
fetch next cur_dxf into @id_i,@billid_i,@billdate_i,
@sumprice_i,
@invcode_i,@batch_i,@packcode_i,@unitcode_i,@packqty_i,
@singleqty_i,@totalqty_i,@whcode_i,@billstate_i,
@invcode_d,@batch_d,@packcode_d,@unitcode_d,@packqty_d,
@singleqty_d,@totalqty_d,@whcode_d,
@billstate_d end --2
close cur_dxf
end --1

752

社区成员

发帖
与我相关
我的任务
社区描述
PowerBuilder 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧