Like all good problems, this one is sporadic and seemingly senseless, but I figured maybe somebody here would have some suggestions on possible causes of the situation to be laid out.
For our app, we have a service that will update a VersionId column for a given record (SQL Server 2008), which is hooked up to the following trigger to populate some tables that hold historical data (when the book was at version X, the support data was in this state):
So the tblSectionVersion record(s) are always there, but, seemingly without reason, sometimes the tblSectionDocumentVersion table will not have any records corresponding to the tblSectionVersion record, which can obviously cause problems when trying to query against these historical records.
This problem has been discovered in our QA environment (of course, one week from prod rollout), but I just queried my local dev db and found two instances of this happening to me, both for the same Book. Since I'm the only one using this db, I don't know how my instances could be chalked up to some content issue. The trigger has not been modified in almost a month and my problem rows were just from earlier this week.
I can run the query now that is supposed to populate the tblSectionDocumentVersion table using the value for VersionId that I know is missing and I do get back the expected values (i.e. adding another where clause of "AND i.VersionId = x").
Any thoughts? Do I need to provide more detail for anyone to provide any potential answers?
EDIT: Not expecting anyone to debug any particular schema issues we may have, I'm more looking for potential problems with how the trigger is structured.
Thanks.
For our app, we have a service that will update a VersionId column for a given record (SQL Server 2008), which is hooked up to the following trigger to populate some tables that hold historical data (when the book was at version X, the support data was in this state):
Code:
ALTER TRIGGER [dbo].[TRIG_tblBook_Update]
ON [dbo].[tblBook]
For UPDATE
AS
BEGIN
SET NOCOUNT ON;
IF (UPDATE (VersionId))
BEGIN
/*ALWAYS WORKS*/
INSERT INTO tblSection (SectionId,VersionId,AgendaItemTypeId,BookId,ItemOrder,ItemIndentLevel,ItemText)
SELECT a.SectionId,i.VersionId,a.AgendaItemTypeId,i.BookId, a.ItemOrder,a.ItemIndentLevel,a.ItemText
FROM inserted i
JOIN tblSection a ON i.BookId = a.BookId
WHERE NOT EXISTS(select * from tblSection where BookId = i.BookId and VersionId = i.VersionId)
AND i.VersionId is not null AND i.VersionId <>0
/*FAILS AT RANDOM*/
INSERT INTO tblSectionDocumentVersion (SectionDocumentId,VersionId,SectionId,FormDocumentID,DocumentOrder)
SELECT d.SectionDocumentId,i.VersionId,a.SectionId,d.FormDocumentID,d.DocumentOrder
FROM inserted i
JOIN tblSection a ON i.BookId = a.BookId
JOIN tblSectionDocument d ON d.SectionId = a.SectionId
WHERE NOT EXISTS(select * from tblSectionDocumentVersion dv
join tblSection av ON av.SectionId=dv.SectionId and av.VersionId = dv.VersionId
where av.BookId=i.BookId and av.VersionId=i.VersionId)
AND i.VersionId is not null AND i.VersionId <>0
...
/*INSERT INTO TWO OTHER TABLES, WHICH (SEEMINGLY) ALWAYS WORKS, AGAIN USING inserted IN THE QUERIES TO POPULATE THE NEW ROWS*/
END
END
So the tblSectionVersion record(s) are always there, but, seemingly without reason, sometimes the tblSectionDocumentVersion table will not have any records corresponding to the tblSectionVersion record, which can obviously cause problems when trying to query against these historical records.
This problem has been discovered in our QA environment (of course, one week from prod rollout), but I just queried my local dev db and found two instances of this happening to me, both for the same Book. Since I'm the only one using this db, I don't know how my instances could be chalked up to some content issue. The trigger has not been modified in almost a month and my problem rows were just from earlier this week.
I can run the query now that is supposed to populate the tblSectionDocumentVersion table using the value for VersionId that I know is missing and I do get back the expected values (i.e. adding another where clause of "AND i.VersionId = x").
Any thoughts? Do I need to provide more detail for anyone to provide any potential answers?
EDIT: Not expecting anyone to debug any particular schema issues we may have, I'm more looking for potential problems with how the trigger is structured.
Thanks.