Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a point as default value for a geography column?

I have this SQL command that creates a table with a GEOGRAPHY-type column for spatial data that is meant to store a latitude/longitude pair (a point). How can I set a default value for that column? For example, a point at (0,0)?

CREATE TABLE [dbo].[StationLocations] (
    [Id] [int] NOT NULL,
    [StationId] [int] NOT NULL,
    [Position] [Geography],
CONSTRAINT [PKStationLocations] PRIMARY KEY CLUSTERED ([Id]))
like image 724
Rob Avatar asked Jul 17 '14 08:07

Rob


1 Answers

try this:

CREATE TABLE #TEMP(ID INT,COL1 GEOGRAPHY DEFAULT(CONVERT(GEOGRAPHY,'POINT (0 0)')))
INSERT INTO #TEMP (ID) VALUES(1)
SELECT * FROM #TEMP
ID  COL1
1   0xE6100000010C00000000000000000000000000000000

SELECT ID,CONVERT(VARCHAR,COL1) AS DEF_VALUE FROM #TEMP
ID  DEF_VALUE
1   POINT (0 0)

In your case

CREATE TABLE [dbo].[StationLocations] (
    [Id] [int] NOT NULL,
    [StationId] [int] NOT NULL,
    [Position] [Geography] DEFAULT(CONVERT(GEOGRAPHY,'POINT (0 0)')),
CONSTRAINT [PKStationLocations] PRIMARY KEY CLUSTERED ([Id]))
like image 148
Recursive Avatar answered Sep 22 '22 19:09

Recursive