SQL触发器的索链反应.

表A ( id int identity(1,1) not null ,con varchar(30)) ,表B( id int ,con varchar(30) ,表C ( id int identity(1,1) not null,con varchar(30) )

表A有一个 for Insert 的触发器用于向 表B插入历史记录,同时向表C记录日志.

程序用的是 听堂的 SPL 组件.在程序中向表A插入一条记录后,用表A的ID进行其它的操作.但得到表A的ID总是不对.差得很多.搞了两个小时,晕!

我估计,SPL在插入到表A后,返回的表A的ID是用 select @@identity ,这时触发器也对表C执行Insert 操作.影响了 @@identity 的值 .所以得到的表A的ID就错了.

解决方案: 用  SCOPE_IDENTITY () 返回,但是SPL.....
      想办法设置 @@identity 的值.

create table #tab(id int identity(1,1primary key not null )
set identity_insert #tab on
insert into #tab (id) select 100
set identity_insert #tab off
drop table #tab
select @@identity



还有更好的解决方案吗?
附上在解决问题时搜到的贴子.

posted @ 2006-10-13 18:15  NewSea  阅读(312)  评论(0编辑  收藏  举报