DECLARE @g geometry;SET @g = geometry::STGeomFromText('POLYGON((0 2, 2 0, 4 2, 0 2))', 0);SELECT @g.STNumPoints() as PointCount, @g.STBuffer(0.00001).STNumPoints() as countAfterBuffer;

DECLARE @g geometry;SET @g = geometry::STGeomFromText('POLYGON((0 2, 2 0, 4 2, 0 2))', 0);SELECT @g.STNumPoints() as PointCount, @g.STBuffer(0.00001).STNumPoints() as countAfterBuffer,@g.STBuffer(0.00001).Reduce(0.01).STNumPoints() as countAfterReduce;

DECLARE @g geometry;SET @g = geometry::STGeomFromText('POLYGON((0 2, 2 0, 4 2, 0 2))', 0);SELECT 'var L=google.maps.LatLng;' UNION ALL SELECT 'var polyg = [' UNION ALL SELECT 'new L(' + str(@g.STPointN(n).STY, 10,6)+',' + str(@g.STPointN(n).STX, 10,6) +'),' from dbo.Tally where N < @g.STNumPoints() UNION ALLSELECT 'new L(' + str(@g.STPointN(n).STY, 10,6)+',' + str(@g.STPointN(n).STX, 10,6) +')' from dbo.Tally where N = @g.STNumPoints() UNION ALLSELECT '];'

FROM dbo.Tally aCROSS APPLY dbo.Tally b

DECLARE @g geometry;--Create a multi-polygon geometrySET @g = geometry::STGeomFromText('POLYGON((0 2, 2 0, 4 2, 0 2))', 0)SELECT @g = @g.STUnion(geometry::STGeomFromText('POLYGON((3 3, 3 4, 4 4, 3 3))',0));IF @g.STNumGeometries() > 1 PRINT 'Multi-polygon detected'; SELECT c.LineText FROM dbo.tally a CROSS APPLY (SELECT 0 as N, 'var polyg = [' as LineText UNION ALL -- this line appears first, so sort order is zero SELECT b.N, 'new L(' + str(@g.STGeometryN(a.N).STPointN(b.N).STY, 10,6)+',' + str(@g.STGeometryN(a.N).STPointN(n).STX, 10,6) +'),' FROM dbo.Tally b where b.N < @g.STGeometryN(a.N).STNumPoints() UNION ALL SELECT b.N, 'new L(' + str(@g.STGeometryN(a.N).STPointN(b.N).STY, 10,6)+',' + str(@g.STGeometryN(a.N).STPointN(n).STX, 10,6) +')' FROM dbo.Tally b where b.N = @g.STGeometryN(a.N).STNumPoints() -- above select is for the last line of the list, no trailing comma -- following lines sort to appear after each geometry definition list UNION ALL Select @g.STGeometryN(a.N).STNumPoints() + 1, '];' UNION ALL Select @g.STGeometryN(a.N).STNumPoints() + 2, '//map cmds here//;' ) c WHERE a.N <= @g.STNumGeometries() --and a.N = 1order by a.N, c.N;

CREATE TABLE #rings (RegisterID int not null, PartNum int not null, RingNum int not null, Ring Geometry, NumPoints int);ALTER TABLE #rings ADD CONSTRAINT rings_pk PRIMARY KEY (RegisterId, PartNum, RingNum);;WITH e1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), e2(N) AS (SELECT 1 FROM E1 a, E1 b), --100-- e3(N) AS (SELECT 1 FROM E2 a, E2 b), --10000 cteTally(N) AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) N FROM e2 ),-- Get Individual polygons from Geometry GetPolygonParts AS ( SELECT RegisterID, RegisteredExtent.STGeometryN(t.N) Part, RegisteredExtent.STGeometryN(t.N).STNumPoints() NumPoints, t.N PartNum FROM Register a CROSS APPLY (select TOP(a.RegisteredExtent.STNumGeometries()) n from cteTally z ) t WHERE RegisteredExtent is not null ),-- Split to internal and external rings GetRings AS ( SELECT a.RegisterID, a.PartNum, 0 RingNum, Part.STExteriorRing() Ring, Part.STExteriorRing().STNumPoints() NumPoints FROM GetPolygonParts a UNION ALL SELECT a.RegisterID, a.PartNum, t.N RingNum, Part.STInteriorRingN(t.N) Ring, Part.STInteriorRingN(t.N).STNumPoints() NumPoints FROM GetPolygonParts a CROSS APPLY (select TOP(a.Part.STNumInteriorRing()) n from cteTally z ) t )INSERT INTO #rings SELECT * FROM GetRingsCREATE TABLE #coords (RegisterID int not null, PartNum int not null, RingNum int not null, X float not null, Y float not null)ALTER TABLE #coords ADD CONSTRAINT int_pk PRIMARY KEY (RegisterId, PartNum, RingNum, x, y);;WITH e1(N) AS (SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 ), e2(N) AS (SELECT 1 FROM E1 a, E1 b), --100 e3(N) AS (SELECT 1 FROM E2 a, E2 b), --10000 cteTally(N) AS ( SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) N FROM e3 ), GetRingCoords AS ( SELECT RegisterID, p.PartNum, p.RingNum ,p.Ring.STPointN(t.N).STX X, p.Ring.STPointN(t.N).STY Y FROM #rings p CROSS APPLY (select TOP (p.NumPoints - 1) n from cteTally z) t )INSERT INTO #coords SELECT * FROM GetRingCoords