--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
ParentId
,MilestoneType
,MilestoneName
,MilestoneDescription
,RuleTypeId
,RuleValueId
,RuleValue
,Time
,@FolderId
,ExternallyVisible
,Id
FROM WF_MilestoneRule
WHERE AdminFolderId = @AdminFolderId
--Set ParentId
UPDATE WF_MilestoneRule SET [ParentId] = (SELECT [Id] FROM WF_MilestoneRule WHERE [MilestoneCreatedById] = ParentId)
WHERE MilestoneCreatedById IN (SELECT [Id] FROM WF_MilestoneRule WHERE [AdminFolderId] = @AdminFolderId)
AND ParentId IS NOT NULL