IF EXISTS ( select * from sysobjects where type='tr' AND name='Tgr_TrackCode_StateUpdate') BEGIN DROP TRIGGER Tgr_TrackCode_StateUpdate; END GO CREATE TRIGGER Tgr_TrackCode_StateUpdate ON TRC_TrackCode AFTER Update AS BEGIN DECLARE @fromTaskNo varchar(100); DECLARE @toTaskNo varchar(100); INSERT INTO TRC_TrackCode_StateRecord (FromTask, ToTask,RTime,IsCancel, JCode, [Level], Status, PackFlag, RID, RCode, ParentJCode, BoxNo, AppendCode, TaskNo, TaskNo1, EquipNo, EquipNo1, EquipNo2, PrintCount, Remark, SourceFlag, ScanNo, ScanNo2, LineName, ActiveBatchNo, ImportBatchNo, PrintBatchNo, PrintBoxNo, AddBy, AddOn, EditBy, EditOn, FlagDelete, BatchNum1, BatchNum2, Fuzzy, SN) SELECT de.TaskNo, ins.TaskNo, sysdatetime(), 0 IsCancel, de.JCode, de.[Level], de.Status, de.PackFlag, de.RID, de.RCode, de.ParentJCode, de.BoxNo, de.AppendCode, de.TaskNo, de.TaskNo1, de.EquipNo, de.EquipNo1, de.EquipNo2, de.PrintCount, de.Remark, de.SourceFlag, de.ScanNo, de.ScanNo2, de.LineName, de.ActiveBatchNo, de.ImportBatchNo, de.PrintBatchNo, de.PrintBoxNo, de.AddBy, de.AddOn, de.EditBy, de.EditOn, de.FlagDelete, de.BatchNum1, de.BatchNum2, de.Fuzzy, de.SN FROM deleted de JOIN inserted ins ON de.JCode = ins.JCode WHERE de.TaskNo IS NOT NULL AND ins.TaskNo IS NOT NULL AND de.TaskNo<>ins.TaskNo; INSERT INTO TRC_TrackCode_StateRecord (FromTask, ToTask,RTime,IsCancel, JCode, [Level], Status, PackFlag, RID, RCode, ParentJCode, BoxNo, AppendCode, TaskNo, TaskNo1, EquipNo, EquipNo1, EquipNo2, PrintCount, Remark, SourceFlag, ScanNo, ScanNo2, LineName, ActiveBatchNo, ImportBatchNo, PrintBatchNo, PrintBoxNo, AddBy, AddOn, EditBy, EditOn, FlagDelete, BatchNum1, BatchNum2, Fuzzy, SN) SELECT de.TaskNo, ins.TaskNo+'_P4', sysdatetime(), 0 IsCancel, de.JCode, de.[Level], de.Status, de.PackFlag, de.RID, de.RCode, de.ParentJCode, de.BoxNo, de.AppendCode, de.TaskNo, de.TaskNo1, de.EquipNo, de.EquipNo1, de.EquipNo2, de.PrintCount, de.Remark, de.SourceFlag, de.ScanNo, de.ScanNo2, de.LineName, de.ActiveBatchNo, de.ImportBatchNo, de.PrintBatchNo, de.PrintBoxNo, de.AddBy, de.AddOn, de.EditBy, de.EditOn, de.FlagDelete, de.BatchNum1, de.BatchNum2, de.Fuzzy, de.SN FROM deleted de JOIN inserted ins ON de.JCode = ins.JCode WHERE de.PackFlag not in (1,4,5) AND ins.PackFlag in (4,5) AND de.TaskNo=ins.TaskNo ; END GO