Set Identity_Insert map On GO With Data As Select Cast MaxId Id As int

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
Set Identity_Insert map On
GO
With Data As
(
Select Cast(MaxId.Id + 1 As int) As Id
, T.PID
/*, 'Copy Of ' + T.name As Name*/
, T.Name
, T.Id As OldId
, 0 As OldParentId
, 2 AS SSID
, T.URL
, T.AParams
, T.class
, T.PermID
From map As T
Cross Join( Select Max( id ) As Id From map ) As MaxId
Where T.ID = 11641
Union All
Select Cast(Parent.id + Row_Number() Over( Order By Child.Id ) + 1 As int)
, Parent.Id
/*, 'Copy of ' + Child.Name*/
, Child.Name
, Child.Id
, Child.PID
, 2 AS SSID
, Child.URL
, Child.AParams
, Child.class
, Child.PermID
From Data As Parent
Join map As Child
On Child.PID = Parent.OldId
)
/*Insert map( Id, PID, Name, SSID, URL, AParams, class, PermID )*/
Select Id, PID, Name, SSID, URL, AParams, class, PermID
From Data
GO
Set Identity_Insert map Off
GO