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