How to persist a calculated GEOMETRY or GEOGRAPHY column
Asked Answered
F

2

10

I'm trying to create a table under SQL Server 2008 containing a GEOMETRY column and a calculated variation thereof.

Considering the following table where the calculated column returns a buffered geometry:

CREATE TABLE [dbo].[Test] (
    [Geometry]      GEOMETRY    NOT NULL,
    [Buffer]        FLOAT       NOT NULL,
    [BufferedGeometry] AS ([Geometry].STBuffer([Buffer])) PERSISTED
);

The problem with this is it results in the following error:

Msg 4994, Level 16, State 1, Line 2 Computed column 'BufferedGeometry' in table 'Test' cannot be persisted because the column type, 'geometry', is a non-byte-ordered CLR type.

I have search BOL and the web and can't seem to find a solution to my problem. I really would like it to be persisted so I can index it effectively. I could set it in code, but then I have the possibility of inconsistent data as I require both values at some point in time.

Anyone played with this and know a solution or workaround?

Update: Microsoft has added this functionality in SQL Server 2012.

Firedamp answered 23/10, 2008 at 19:0 Comment(2)
Vote for this Connect Item: connect.microsoft.com/SQLServer/feedback/details/378126/…Jemine
The following does not work: ALTER TABLE dbo.Period ADD [Interval] AS (geometry::STGeomFromText( 'LINESTRING (0 ' + CAST(CAST(DATEDIFF(SECOND, '19700101', StartDate) AS BIGINT) AS VARCHAR(20)) + ', 0 ' + CAST(CAST(DATEDIFF(SECOND, '19700101', EndDate) AS BIGINT) AS VARCHAR(20)) + '''', 0)) PERSISTED ;Heteroousian
B
4

I guess you could use a trigger to calculate it and store it to the [BufferedGeometry] field

Burne answered 23/10, 2008 at 22:18 Comment(2)
Personally I don't like using triggers in databases, I haven't needed to use one now for many years and I hopefully won't need to resort to one.Firedamp
I agree - but sometimes it is the only way to go :-)Burne
I
1

Whoever is still having such problem: SQL Server 2012 now allows it

Impuissant answered 24/7, 2013 at 12:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.