The tables have no primary keys, no fk references.The Docdetail_edit table is hard coded to store a fixed number of status change dates,and only the last date for each change of given status.The trigger logic becomes unecessarily complex and the flexibility suffers from the limitations of the current design.A more normalized approach would be1. more flexible2. easier to implement3. enable the storage of all status changes to a document.Example:-- the tablescreate table docs( docno char(10) primary key, status int not null)go-- table that is fed by trigger and tracks all statuschanges to docscreate table docs_trackstatus( docno char(10) references docs(docno), statusdate datetime not null, status int not null, primary key(docno,statusdate,status))go-- trigger for insert, inserts all new docs into the tracking tablecreate trigger trgInsert_docs_trackstatus on docs for insert asbegin insert docs_trackstatus( docno, statusdate, status ) select inserted.docno, getdate(), inserted.status from insertedendgo-- trigger for update, inserts new posts into the tracking tablecreate trigger trgUpdate_docs_trackstatus on docs for update asbegin if update(status) insert docs_trackstatus( docno, statusdate, status ) select inserted.docno, getdate(), inserted.status from inserted join deleted on inserted.docno = deleted.docno where inserted.status <> deleted.statusendgo-- insert sample datainsert docs select 'a', 0 union select 'b', 0update docs set status = status + 1 where docno = 'b'-- a pivot / select to retrieve the dates for status changes-- in tabular formselect docno, max(case status when 0 then statusdate end) as date0, max(case status when 1 then statusdate end) as date1, max(case status when 2 then statusdate end) as date2from docs_trackstatusgroup by docnogo-- cleanupdrop table docs_trackstatusdrop table docs
rockmoose