IF EXISTS(SELECT *
	FROM sys.tables
	WHERE name = 'geomcollection' AND
	schema_id = SCHEMA_ID('dbo'))
	DROP table dbo.geomcollection
GO


SET NOCOUNT ON

-- Create the numbers table.
DECLARE @counter INTEGER
DECLARE @numberTable TABLE(Number INTEGER PRIMARY KEY)
SELECT @counter = 1
WHILE @counter <= 10000
BEGIN
    INSERT INTO @numberTable(number) VALUES (@counter)

    SELECT @counter = @counter + 1
END


-- Now extract the data.

SELECT
 a.guid,'SharedGeom' as source,
 a.geom.STGeometryN(b.number) AS Shape,
 a.geom.STGeometryN(b.number).ToString() AS WellKnownText
 into GeomCollection
FROM sharedgeom a 
    INNER JOIN @numberTable b ON b.number <= a.geom.STNumGeometries()
WHERE a.guid = '89F44DC0-0744-4A94-A351-FB7516DB3A3C' and a.geom.STGeometryN(b.number).ToString() like 'polygon%'