編輯記錄保存時,而導致“找不到更新的資料列,最後讀取值已變更“的錯誤。實際上已影響到數據表的記錄,而且觸發器也成功的執行了,但是還是會出這個錯誤。
我將AdoQuery的CursorLocation設為clUseServer則可以正常保存不會報這個錯,可是這樣將不能編輯多行記錄了,如果用clUseClient則會提示如上的錯誤。
--如何用ADOQUERY操作帶有觸發器的表?
我將AdoQuery的CursorLocation設為clUseServer則可以正常保存不會報這個錯,可是這樣將不能編輯多行記錄了,如果用clUseClient則會提示如上的錯誤。
--如何用ADOQUERY操作帶有觸發器的表?
调用RvProject1的打印啊
不需要预浏
直接点Button1打印!
FOR UPDATE
AS
/*
--edit: skm
--date: 2005.06.27
.修改庫存計劃表明細,同步更新庫存總帳表TStockBook的計劃數,
.主從表為外鍵約束,級聯更新
*/
---------------------------
declare @fperiod varchar(6)select @fperiod = fvalue from TparamKeys where fcode = '50101' --當前庫存期間
--刪除--------------------------
--更新庫存總帳計劃入庫數
update a
set a.fperiod = @fperiod, a.fqtyip = isnull(a.fqtyip, 0) - b.sum_qty_in
from TStockBook a, (select flocation_in, fitem, sum_qty_in = sum(isnull(fqty,0)) from deleted group by flocation_in, fitem) b
where a.flocation = b.flocation_in and a.fitem = b.fitem
--更新庫存總帳計劃出庫數
update a
set a.fperiod = @fperiod, a.fqtyop = isnull(a.fqtyop, 0) - b.sum_qty_out
from TStockBook a, (select flocation_out, fitem, sum_qty_out = sum(isnull(fqty,0)) from deleted group by flocation_out, fitem) b
where a.flocation = b.flocation_out and a.fitem = b.fitem
--新增----------------------
--更新庫存總帳計劃入庫數
update a
set a.fperiod = @fperiod, a.fqtyip = isnull(a.fqtyip, 0) + b.sum_qty_in
from TStockBook a, (select flocation_in, fitem, sum_qty_in = sum(isnull(fqty,0)) from inserted group by flocation_in, fitem) b
where a.flocation = b.flocation_in and a.fitem = b.fitem
--更新庫存總帳計劃出庫數
update a
set a.fperiod = @fperiod, a.fqtyop = isnull(a.fqtyop, 0) + b.sum_qty_out
from TStockBook a, (select flocation_out, fitem, sum_qty_out = sum(isnull(fqty,0)) from inserted group by flocation_out, fitem) b
where a.flocation = b.flocation_out and a.fitem = b.fitem
---------------
這是我原來的觸發器,發現直接操作庫存計劃表TStockPlan_Hd時,就會報上述的錯誤。
我後來改為以下就沒有上述問題了,請看一下這樣有什麼問題沒?
CREATE TRIGGER [TStockPlan_dtl_upd] ON [dbo].[TStockPlan_dtl]
FOR UPDATE
AS
/*
--edit: skm
--date: 2005.06.30
.修改庫存計劃表明細,同步更新庫存總帳表TStockBook的計劃數,
.主從表為外鍵約束,級聯更新
*/
---------------------------
commit --先觸發declare @fperiod varchar(6)select @fperiod = fvalue from TparamKeys where fcode = '50101' --當前庫存期間BEGIN TRANif exists( select name from tempdb..sysobjects where id = object_id(N'[tempdb]..[#grp_deleted]') and xtype = 'U')
drop table #grp_deleted
select ftype = convert( tinyint, 1), flocation = flocation_in, fitem, sum_qty = sum(isnull(fqty,0))
into #grp_deleted
from deleted
group by flocation_in, fitem insert into #grp_deleted
select ftype = convert( tinyint, 0), flocation = flocation_out, fitem, sum_qty = sum(isnull(fqty,0))
from deleted
group by flocation_out, fitem if exists( select name from tempdb..sysobjects where id = object_id(N'[tempdb]..[#grp_inserted]') and xtype = 'U')
drop table #grp_inserted
select ftype = convert( tinyint, 1), flocation = flocation_in, fitem, sum_qty = sum(isnull(fqty,0))
into #grp_inserted
from inserted
group by flocation_in, fitem insert into #grp_inserted
select ftype = convert( tinyint, 0), flocation = flocation_out, fitem, sum_qty = sum(isnull(fqty,0))
from inserted
group by flocation_out, fitem
--更新庫存計劃入庫數update a
set a.fperiod = @fperiod, a.fqtyip = isnull(a.fqtyip, 0) - b.sum_qty
from TStockBook a, #grp_deleted b
where a.flocation = b.flocation and a.fitem = b.fitem and b.ftype = 1update a
set a.fperiod = @fperiod, a.fqtyip = isnull(a.fqtyip, 0) + b.sum_qty
from TStockBook a, #grp_inserted b
where a.flocation = b.flocation and a.fitem = b.fitem and b.ftype = 1--更新庫存總帳計劃出庫數
update a
set a.fperiod = @fperiod, a.fqtyop = isnull(a.fqtyop, 0) - b.sum_qty
from TStockBook a, #grp_deleted b
where a.flocation = b.flocation and a.fitem = b.fitem and b.ftype = 0update a
set a.fperiod = @fperiod, a.fqtyop = isnull(a.fqtyop, 0) + b.sum_qty
from TStockBook a, #grp_inserted b
where a.flocation = b.flocation and a.fitem = b.fitem and b.ftype = 0drop table #grp_deleted
drop table #grp_insertedif @@error <> 0
ROLLBACK TRAN
else
COMMIT TRAN
如何寫一個表更新更一個表的數據觸發器?為什麼直接引用Inserted ,Deleted表會出這樣的錯誤?
原因,觸發器在后台又操作了其它数据