触发器不能捕捉到更新的信息

None.gif 现象:当更新一个表后,需要将更新的这条纪录信息保存下来.故采用了一个触发器,但在后面的执行过程中,触发器捕捉到的信息不全,本来要捕捉到四个字段的信息,现在只捕捉到三个字段.
None.gif 分析: 按理论是应该能捕捉到的,如果能捕捉到三个字段信息,说明此触发器已正常工作.应该是被捕捉对象的信息过时早丢失.后查是程序中在执行对该表中的该条记录进行更新后,马上删除了该条记录,造成对该纪录的其余信息捕捉不全.
None.gif
None.gif解决: 增加更新与删除该条记录的时间差,让触发器在对该记录进行更新后有更多的时间去捕捉该条记录的相关信息.
None.gif
None.gif
-- 创建用于监视对TestResult表进行更新数据的触发器
None.gif
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[TestResult_Update] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
None.gif
drop   trigger   [ dbo ] . [ TestResult_Update ]
None.gif
go
None.gif
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[TestResult_ServerUpdate] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
None.gif
drop   trigger   [ dbo ] . [ TestResult_ServerUpdate ]
None.gif
None.gif
GO
None.gif
CREATE   TRIGGER  TestResult_ServerUpdate  ON  TestResult AFTER   UPDATE
None.gif
AS
None.gif
-- IF UPDATE (CPH)
None.gif
BEGIN
None.gif
DECLARE   @UniqID   uniqueidentifier ,
None.gif
@id   bigint ,
None.gif
@StationCode   nvarchar ( 50 ),
None.gif
@TestLineCode     nvarchar ( 50 )       -- 检测线代码
None.gif
DECLARE  c3  CURSOR   FOR
None.gif   
-- SELECT TestResult.AutoRTID,TestResult.ResultUniqID,TestResult.TestLineCode,TestResult.StationNo 
None.gif
    -- FROM   TestResult, deleted
None.gif
    -- WHERE  TestResult.AutoRTID = deleted.AutoRTID  and TestResult.StationNo= deleted.StationNo and TestResult.ResultUniqID=deleted.ResultUniqID and TestResult.TestLineCode=deleted.TestLineCode
None.gif
   SELECT  AutoRTID,ResultUniqID,TestLineCode,StationNo   FROM  DELETED
None.gif
OPEN  c3
None.gif
FETCH   NEXT   FROM  c3  INTO   @id , @UniqID , @TestLineCode , @StationCode
None.gif
WHILE   @@fetch_status   =   0
None.gif
BEGIN
None.gif  
INSERT   INTO  Server_UpdateRecordTable (TableName,NumOfUniqRecord,NumOfRecordID,NumOfCheckStation,NumOfCheckGroup)  VALUES ( ' TestResult ' , @UniqID , @id , @StationCode , @TestLineCode )
None.gif  
FETCH   NEXT   FROM  c3  INTO   @id , @UniqID , @TestLineCode , @StationCode
None.gif
END
None.gif
CLOSE  c3
None.gif
DEALLOCATE  c3
None.gif
END
None.gif
GO
None.gif
None.gif
None.gif
-- 测试
None.gif--
select * from testresult
None.gif--
select * from server_updaterecordtable
None.gif

None.gif
-- update testresult set autortid=99999 where autortid=99999
None.gif--
delete from testresult where autortid=99999
None.gif--
go
None.gif--
drop trigger savedel
None.gif--
go
None.gif--
CREATE TRIGGER savedel
None.gif--
   ON testresult
None.gif--
FOR DELETE
None.gif--
AS
None.gif--
   INSERT INTO Server_UpdateRecordTable (NumOfUniqRecord,NumOfRecordID,NumOfCheckStation)
None.gif--
   SELECT resultuniqid,autortid,stationcode FROM deleted
None.gif--
go
None.gif

None.gif
None.gif
-- 创建监视插入Server_InsertRecordTable表中数据的触发器
None.gif--
用于更新Server_UpdateRecordTable 的纪录唯一编号,好用于后面的更新操作
None.gif
if   exists  ( select   *   from  dbo.sysobjects  where  id  =   object_id (N ' [dbo].[InsertRecordTable_ServerInsert] ' and   OBJECTPROPERTY (id, N ' IsTrigger ' =   1 )
None.gif
drop   trigger   [ dbo ] . [ InsertRecordTable_ServerInsert ]
None.gif
go
None.gif
CREATE   TRIGGER  InsertRecordTable_ServerInsert  ON  Server_InsertRecordTable
None.gif
FOR   INSERT
None.gif
AS
None.gif
DECLARE  
None.gif
@UniqID   uniqueidentifier -- 在服务器上插入纪录后的生成的惟一的纪录编号 
None.gif
@id   bigint ,                        -- 在检测站的数据库中的纪录编号
None.gif
@StationCode      nvarchar ( 50 ),      -- 检测站代码 
None.gif
@TestLineCode     nvarchar ( 50 )       -- 检测线代码
None.gif
DECLARE  c2  CURSOR   FOR
None.gif   
SELECT  Server_InsertRecordTable.NumOfUniqRecord,inserted.NumOfRecordID,inserted.NumOfCheckStation,inserted.NumOfCheckGroup
None.gif   
FROM    Server_InsertRecordTable, inserted
None.gif   
WHERE   Server_InsertRecordTable.NumOfUniqRecord  =  inserted.NumOfUniqRecord  and  Server_InsertRecordTable.NumOfRecordID =  inserted.NumOfRecordID  and  Server_InsertRecordTable.NumOfCheckStation = inserted.NumOfCheckStation  and  Server_InsertRecordTable.NumOfCheckGroup = inserted.NumOfCheckGroup
None.gif
OPEN  c2
None.gif
FETCH   NEXT   FROM  c2  INTO    @UniqID @id , @StationCode , @TestLineCode
None.gif
WHILE   @@fetch_status   =   0
None.gif
BEGIN
None.gif    
UPDATE   Server_UpdateRecordTable  set  NumOfUniqRecord = @UniqID    WHERE  NumOfRecordID = @id   and  NumOfCheckStation = @StationCode   and  NumOfCheckGroup = @TestLineCode
None.gif    
FETCH   NEXT   FROM  c2  INTO   @UniqID , @id , @StationCode , @TestLineCode
None.gif
END
None.gif
CLOSE  c2
None.gif
DEALLOCATE  c2
None.gif
GO
None.gif
None.gif
select   top   3   *   from  testresult
None.gif
select   top   3   *   from  server_insertrecordtable
None.gif
select   *   from  Server_UpdateRecordTable
None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值