--Insert the values from the selected payroll into the same table
INSERT INTO WF_MilestoneRule --Id column automatically generated
(ParentId
,MilestoneType
,MilestoneName
,MilestoneDescription
,RuleTypeId
,RuleValueId
,RuleValue
,Time
,AdminFolderId
,ExternallyVisible
,MilestoneCreatedById) -- Id of the column that has been copied from
SELECT
0
,MilestoneType
,MilestoneName
,MilestoneDescription
,RuleTypeId
,RuleValueId
,RuleValue
,Time
,@FolderId
,ExternallyVisible
,Id
FROM WF_MilestoneRule t1
WHERE t1.AdminFolderId = @AdminFolderId
--Set ParentId
UPDATE WF_MilestoneRule
SET [ParentId] = (SELECT t2.ID
FROM WF_MilestoneRule t2 join WF_MilestoneRule t3 on t2.ParentID = t3.MilestoneCreatedById
WHERE t1.MilestoneCreatedById = t2.ID )
FROM WF_MilestoneRule t1
WHERE t1.AdminFolderId = @FolderId
AND t1.ParentID = 0