INSERT INTO Service2
(
ServiceName,
SourceId
)
SELECT ServiceName,
SourceId = STUFF(
(
SELECT ',' + SourceId
FROM (
SELECT DISTINCT ServiceName,
SourceId
FROM [Service]
) t
WHERE ServiceName = s.ServiceName FOR XML PATH('')
),
1,
1,
''
)
FROM (
SELECT DISTINCT ServiceName,
SourceId
FROM [Service]
) s
GROUP BY
ServiceName