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]))
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]))
Not sure if this is the best method but, using this answer as a reference, you could define it like this :
ALTER TABLE dbo.StationLocations
ADD CONSTRAINT DF_StationLocations_Position
DEFAULT geography::STPointFromText('POINT (0 0)', 4326) FOR Position
;
I don't think you can do it directly. You could use an insert trigger in a roundabout way, but it's not a completely satisfactory solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.