So, I feel the need to be recorded, so that after the encounter this problem again, and get answers from the idea.
In the flip-flop, the master table corresponding to a data call list when the data is inserted
http://bbs.csdn.net/topics/390631301
Hello everyone, as Kingdee ERP can not automatically calculated based on the number of spare spare rate quote inside. So it is necessary to build a trigger from.
My trigger is built on the list, call list when inserted into the corresponding data in the main table data, key data is then the main table did not seem committed to the database, is there any way to call, thank you.
my reply:
By the following experiment, verified, and if the transaction has not been submitted to the primary table, but want the main table with the corresponding data in the flip-flop, to calculate spare, is entirely possible.
Because the trigger is triggered and the operation, in the same transaction.
As long as you insert the main table, insert the list, in the same transaction, and it will also trigger the same transaction, although it did not submit the master table operation, but also in the query to trigger this has not master table records submitted.
-
--主表
-
create
table t1
-
(
-
id
int primary
key,
-
v
varchar(
20)
-
)
-
-
--明细表
-
create
table t2
-
(idd
int primary
key,
-
id
int
references t1(
id),
-
vv
varchar(
20)
-
)
-
-
insert
into t1
-
select
1,
'aa'
-
-
insert
into t2
-
select
1,
1,
'xxx'
-
union all
select
2,
1,
'yyy'
-
-
go
-
-
-
create
trigger dbo.trigger_t2
-
on dbo.t2
-
for
insert
-
as
-
-
--查询主表t1,发现刚才插入数据的事务,虽然没有提交
-
--但是在触发器中,完全可以查询到刚才插入到主表的数据
-
select *
from t1
-
-
--下面可以引用t1表,也就是主表,和明细表,来计算出备品数
-
-
go
-
-
-
begin tran
-
-
-
insert
into t1
-
select
2,
'bb'
-
-
insert
into t2
-
select
3,
2,
'123'
-
-
--先不提交事务
-
--commit
-
/*
-
id v
-
1 aa
-
2 bb
-
*/